AlgoViz

Doubly linked list

easy 16 min

A doubly linked list is a chain of nodes where every node links to both its neighbours, the one before (prev) and the one after (next).

See it run

A doubly linked list, one operation at a time: every pointer change, the code line that does it, and what it costs.

Operations

Insert at head

Create a node, link its next to the current head, point the head's prev back at it, then move head.

Step 1 of 5

The list before

NULL ← HEAD → 10 ⇄ 20 ⇄ 30 ← TAIL → NULL. We insert 5 at the head.

Step 1: The list before

def insert_head(self, value):    new_node = Node(value)    if self.head:        new_node.next = self.head   # point forward to old head        self.head.prev = new_node   # old head points back    self.head = new_node    if not self.tail:               # list was empty        self.tail = new_node
Time
O(1)
Space
O(1)
vs array
Better
vs singly linked
Same

Same O(1) as a singly linked list, but don't forget the prev pointer.

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Forgetting the prev pointer

    critical

    A doubly linked list must update next and prev. Miss one and walking in that direction breaks.

    Wrong
    new_node.next = headhead = new_node# head.prev is still None
    Right
    new_node.next = headif head:    head.prev = new_nodehead = new_node
  • Not moving head or tail

    critical

    Both head and tail must be updated whenever they change.

    Wrong
    last.next = new_nodenew_node.prev = last# tail still points at the old last node
    Right
    last.next = new_nodenew_node.prev = lasttail = new_node
  • Wrong order of pointer updates

    critical

    Link the new node before you move head or tail, or you lose the old reference.

    Wrong
    head = new_node        # old head is lostnew_node.next = head   # points at itself
    Right
    new_node.next = headif head:    head.prev = new_nodehead = new_node
  • Single-node list

    major

    With one node, head and tail are the same node. Deleting it must clear both.

    Wrong
    if head == tail:    head = None    # tail still points at the deleted node
    Right
    if head == tail:    head = None    tail = None
  • Treating it as circular

    major

    A regular doubly linked list ends at NULL; a circular one loops back to the head.

    Wrong
    while current:            # fine for regular    current = current.next  # loops forever if circular
    Right
    while current is not None:   # regular    current = current.next# circular: stop when current == head
  • Not clearing deleted node references

    minor

    Clear next and prev on a removed node so nothing keeps it alive by accident.

    Wrong
    removed = currentcurrent = current.next# removed.next / removed.prev still set
    Right
    removed = currentcurrent = current.nextremoved.next = Noneremoved.prev = None
  • Always walking from the head

    minor

    You can start from either end. Choose the closer one.

    Wrong
    current = headfor _ in range(pos):    current = current.next
    Right
    if pos < length // 2:    walk from head with nextelse:    walk from tail with prev
  • Skipping sentinel nodes

    minor

    Dummy head and tail nodes remove most null checks; many production lists use them.

    Wrong
    if head is None: ...elif pos == 0: ...else: ...
    Right
    # dummy_head <-> nodes <-> dummy_tail# always insert between two real links

Interview tips

  • Reach for it when

    • LRU cache: move an entry to the front on access
    • Browser history: back and forward
    • Undo and redo
    • A music player's previous and next
    • Text editor cursor movement
  • Avoid it when

    • You only walk forward: use a singly linked list
    • Memory is tight: one pointer per node is cheaper
    • You need random access: use an array
    • Cache locality matters: arrays are contiguous
  • Doubly vs singly

    • Delete tail: O(1) vs O(n)
    • Insert at tail: O(1) vs O(n) without a tail pointer
    • Memory: two pointers per node
    • Backward traversal: doubly only
    • More edge cases to get right
  • Interview patterns

    • LRU cache: hash map + doubly linked list
    • Flatten a multilevel doubly linked list
    • Convert a BST to a sorted doubly linked list
    • Design browser history
    • Implement a deque