A doubly linked list is a linked list where each node points in two directions.
Each node stores:
valuenextprev
null <- [12] <-> [24] <-> [36] -> null
Why add prev?
A singly linked list can only move forward. A doubly linked list can move forward and backward.
That extra link makes some operations easier, but each node uses more memory and every mutation must update two directions correctly.
Insert between two nodes
function insertBetween(left, right, node) {
node.prev = left;
node.next = right;
left.next = node;
right.prev = node;
}
Where doubly linked lists show up
- LRU cache
- Browser history
- Music playlist navigation
- Editor cursor movement
- Deques
Complexity
- Move next:
O(1) - Move previous:
O(1) - Search:
O(n) - Insert near known node:
O(1) - Delete known node:
O(1)
Practice links
Use the visualizer below to move in both directions.
Interactive walkthrough
Doubly Linked List Traversal
Move forward and backward using next and prev links.