Delete a node in O(1) given the node reference
JavaScriptdeleteNode(node) {
if (node.prev) {
node.prev.next = node.next;
} else {
this.head = node.next;
}
if (node.next) {
node.next.prev = node.prev;
} else {
this.tail = node.prev;
}
node.prev = null;
node.next = null;
this.size--;
}Explanation
The code reconnects the predecessor and successor directly. No predecessor search is needed, because node.prev already points to it. That is the key practical advantage of a doubly linked list.
Output
For 10 <-> 25 <-> 37 <-> 52, deleting node 37 produces 10 <-> 25 <-> 52
Real-life Example
In an LRU cache, a recently accessed item may need to be removed from the middle and moved to the front instantly. Doubly linked lists make that constant-time pointer update possible.