See Algorithms
SEE ALGORITHMS

Doubly Linked List Visualization

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.

Visualizer



Pseudocode

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

Key Properties

  • Bidirectional Traversal: Each node maintains two pointers (next and prev), enabling efficient traversal in both forward and reverse directions.
  • Dynamic Allocation: Nodes are stored independently across heap memory and connected via pointers, eliminating the need for large contiguous memory blocks.
  • Sequential Access: Direct indexing is not supported; finding an item requires linear traversal in O(n) time, though searches can start from either the head or tail depending on which is closer.
  • Efficient Node Removal: Any node can be unlinked in strictly constant O(1) time given its direct reference, without traversing to find the preceding node.
  • Memory Overhead: Each node requires extra storage for two pointer references alongside the data value, compared to a singly linked list.

When to Use

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).

Time & Space Complexity

Metric / Operation
Complexity
Description
Access by IndexO(n)Can traverse from head forward or tail backward, taking O(n) worst-case steps.
Search by ValueO(n)Linear scan through the chain comparing values.
Insert / Delete at EndsO(1)Updates head or tail references and neighboring pointers in constant time.
Delete Given Node PointerO(1)Immediately links node.prev to node.next without traversing to find the predecessor.
Insert / Delete at IndexO(n)Requires traversing to the index before executing the O(1) pointer updates.
Space ComplexityO(n)Requires two pointer references (prev and next) per node, doubling pointer overhead.

Common Interview Questions

What are the main advantages and memory trade-offs of a Doubly Linked List?

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).

How is a Doubly Linked List used in conjunction with a Hash Map to implement an LRU Cache?

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.

How do you reverse a Doubly Linked List in-place?

Traverse through the list node by node. For each node, swap its prev and next pointers:

Code Snippet
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;

💬  Discussion

to join the discussion


Curious to Learn More?

Hand-picked resources to deepen your understanding


Learn DSA on Udemy
Learn DSA on Udemy
As an Udemy Associate, I earn from qualifying purchases.

© 2025 See Algorithms. Code licensed under MIT, content under CC BY-NC 4.0