/** * Adds a node to the beginning of the list. * * @param node the node to add to the beginning of the list. */ public LinkedListNode addFirst(LinkedListNode node) { node.next = head.next; node.previous = head; node.previous.next = node; node.next.previous = node; return node; }
/** Erases all elements in the list and re-initializes it. */ public void clear() { // Remove all references in the list. LinkedListNode node = getLast(); while (node != null) { node.remove(); node = getLast(); } // Re-initialize. head.next = head.previous = head; }
/** Creates a new linked list. */ public LinkedList() { head.next = head.previous = head; }
/** Removes this node from the linked list that it is a part of. */ public void remove() { previous.next = next; next.previous = previous; }