logo
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

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.


💬  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