Linear Data Structures - Doubly Linked Lists Cheatsheet - Codecademy
Linear Data Structures - Doubly Linked Lists Cheatsheet - Codecademy
newTail.setPreviousNode(currentTail);
}
this.tail = newTail;
if (this.head == null) {
this.head = newTail;
}
}
Adding to the Head
currentHead.setPreviousNode(newHead);
newHead.setNextNode(currentHead);
}
this.head = newHead;
if (this.tail == null) {
this.tail = newHead;
}
}
if (this.tail != null) {
this.tail.setNextNode(null);
}
if (removedTail == this.head) {
this.removeHead();
}
return removedTail.data;
}
Removing the Head
if (this.head != null) {
this.head.setPreviousNode(null);
}
if (removedHead == this.tail) {
this.removeTail();
}
return removedHead.data;
}
if (currentHead != null) {
currentHead.setPreviousNode(newHead);
newHead.setNextNode(currentHead);
}
this.head = newHead;
if (this.tail == null) {
this.tail = newHead;
}
}
if (currentTail != null) {
currentTail.setNextNode(newTail);
newTail.setPreviousNode(currentTail);
}
this.tail = newTail;
if (this.head == null) {
this.head = newTail;
}
}
if (removedHead == null) {
return null;
}
this.head =
removedHead.getNextNode();
if (this.head != null) {
this.head.setPreviousNode(null);
}
if (removedHead == this.tail) {
this.removeTail();
}
return removedHead.data;
}
if (removedTail == null) {
return null;
}
this.tail =
removedTail.getPreviousNode();
if (this.tail != null) {
this.tail.setNextNode(null);
}
if (removedTail == this.head) {
this.removeHead();
}
return removedTail.data;
}
if (nodeToRemove == null) {
return null;
}
if (nodeToRemove == this.head) {
this.removeHead();
} else if (nodeToRemove == this.tail)
{
this.removeTail();
} else {
Node nextNode =
nodeToRemove.getNextNode();
Node previousNode =
nodeToRemove.getPreviousNode();
nextNode.setPreviousNode(previousNode);
previousNode.setNextNode(nextNode);
}
return nodeToRemove;
}
}
Removing by Data
if (nodeToRemove == null) {
return null;
}
if (nodeToRemove == this.head) {
this.removeHead();
} else if (nodeToRemove == this.tail)
{
this.removeTail();
} else {
Node nextNode =
nodeToRemove.getNextNode();
Node previousNode =
nodeToRemove.getPreviousNode();
nextNode.setPreviousNode(previousNode);
previousNode.setNextNode(nextNode);
}
return nodeToRemove;
}
Updated Node Class
Doubly linked lists in Java utilize an updated Node public class Node {
class that has a pointer to the previous node. This
comes with additional setter and getter methods for
accessing the previous node. public String data;
private Node next;
private Node previous;
Print Share