Doubly Linked Lists
Add a single extra pointer — pointing backwards — and you unlock O(1) deletion of any node you already have a reference to, bidirectional traversal, and the internals of every LRU cache ever built.
What is a doubly linked list?
In a doubly linked list each node points to both the next and the previous node. You can walk in both directions and remove a node in O(1) when you already have a reference to it, because you can reach its neighbours directly. It's the structure behind LRU caches, browser history and undo/redo.
class Node { constructor(v) { this.value = v; this.prev = null; this.next = null; } }
function remove(node) { // O(1) given the node
if (node.prev) node.prev.next = node.next;
if (node.next) node.next.prev = node.prev;
}Node anatomy
Each node now has three fields: value, next, and prev. The head's prev is null; the tail's next is null. Every other node knows both its neighbour.
prev: null
42
next: 0x200
prev: 0x100
17
next: 0x350
prev: 0x200
93
next: null
Live demo — try the operations
Insert at head or tail in O(1) when you have the tail reference. Delete head in O(1) — the same logic extends to any node once you hold its reference, which is the doubly-linked-list superpower.
Singly Linked List
length: 4The O(1) deletion superpower
In a singly linked list, deleting a node requires its predecessor. In a doubly linked list, any node already knows its predecessor via prev. No traversal needed.
1deleteNode(node): // O(1) — no traversal2 if node.prev != null:3 node.prev.next <- node.next4 else:5 head <- node.next // node was the head6 if node.next != null:7 node.next.prev <- node.prev8 else:9 tail <- node.prev // node was the tail1011insertAfter(node, value): // O(1)12 newNode <- Node(value)13 newNode.prev <- node14 newNode.next <- node.next15 if node.next != null: node.next.prev <- newNode16 node.next <- newNode
LRU Cache — the killer application
An LRU (Least Recently Used) cache evicts the longest-idle item when full. The classic O(1) implementation combines a doubly linked list with a hash map:
Maps each key to its node in the linked list. O(1) lookup to find any node directly — no traversal.
Maintains access order. Most-recently-used node at head. Least-recently-used node at tail.
Look up node via hash map (O(1)), then move it to the head (O(1) — because we have the node reference). Total: O(1).
Remove the tail node (O(1)), delete its key from hash map (O(1)), insert new node at head (O(1)), add to hash map (O(1)). Total: O(1).
Time complexity
Constant time
Constant time
Constant time
Linear time
Linear time
🎯 Quick Check
You have a doubly linked list used as an LRU cache. A key is accessed. What operations happen?
Doubly Linked Lists: summary
Key points
- Each node costs an extra pointer of memory.
- Sentinel (dummy) head and tail nodes remove most edge cases for empty lists.
- Combined with a hash map, it gives O(1) LRU caches.
Frequently asked questions
›When should I use a doubly linked list instead of a singly linked list?
When you need to delete arbitrary nodes quickly or traverse backwards, for example in an LRU cache or a browser history.