A Doubly Linked List is a linear data structure where elements are stored in nodes, and each node points to both the next node and the previous node in the sequence. This allows for traversal in both directions, making certain operations more efficient compared to a singly linked list, at the cost of extra memory for the previous pointer.
function insertAtHead(value):
node = new Node(value)
node.next = head.next
node.prev = head
if head.next is not null:
head.next.prev = node
head.next = node
function insertAtTail(value):
node = new Node(value)
cur = head
while cur.next is not null:
cur = cur.next
cur.next = node
node.prev = cur
function insertAt(index, value):
if index == 0:
insertAtHead(value)
return
cur = head
for i = 1 to index:
if cur.next is null: break
cur = cur.next
node = new Node(value)
node.next = cur.next
node.prev = cur
if cur.next is not null:
cur.next.prev = node
cur.next = node
function deleteAt(index):
cur = head
for i = 0 to index:
prev = cur
cur = cur.next
if cur.next is not null:
cur.next.prev = prev
prev.next = cur.next
next and prev), enabling efficient traversal in both forward and reverse directions.Doubly Linked Lists are essential whenever bidirectional navigation is required or when items must be removed in O(1) time without knowing their predecessor. They are the foundation of LRU (Least Recently Used) Caches (paired with a hash map to reorder nodes upon access in O(1) time), browser history navigation (forward and back buttons), undo-redo managers in graphic and text editors, and double-ended queues (Deques).
Metric / Operation | Complexity | Description |
|---|---|---|
| Access by Index | O(n) | Can traverse from head forward or tail backward, taking O(n) worst-case steps. |
| Search by Value | O(n) | Linear scan through the chain comparing values. |
| Insert / Delete at Ends | O(1) | Updates head or tail references and neighboring pointers in constant time. |
| Delete Given Node Pointer | O(1) | Immediately links node.prev to node.next without traversing to find the predecessor. |
| Insert / Delete at Index | O(n) | Requires traversing to the index before executing the O(1) pointer updates. |
| Space Complexity | O(n) | Requires two pointer references (prev and next) per node, doubling pointer overhead. |
Advantage: Enables bidirectional traversal and O(1) node deletion when a node reference is given (no need to scan for the previous node). Trade-off: Each node requires extra memory to store a prev pointer (8 bytes on 64-bit systems).
The Hash Map maps keys to Doubly linked list nodes for O(1) key lookup. The Doubly Linked List maintains access order: recently accessed nodes are moved to the head in O(1) time, and the least recently used node is evicted from the tail in O(1) time.
Traverse through the list node by node. For each node, swap its prev and next pointers:
let cur = head, temp = null;
while (cur) {
temp = cur.prev;
cur.prev = cur.next;
cur.next = temp;
cur = cur.prev; // advance using swapped prev
}
if (temp) head = temp.prev;to join the discussion
Hand-picked resources to deepen your understanding
© 2025 See Algorithms. Code licensed under MIT, content under CC BY-NC 4.0