Doubly linked list
easy 16 minA 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
criticalA doubly linked list must update next and prev. Miss one and walking in that direction breaks.
Wrongnew_node.next = headhead = new_node# head.prev is still NoneRightnew_node.next = headif head: head.prev = new_nodehead = new_nodeNot moving head or tail
criticalBoth head and tail must be updated whenever they change.
Wronglast.next = new_nodenew_node.prev = last# tail still points at the old last nodeRightlast.next = new_nodenew_node.prev = lasttail = new_nodeWrong order of pointer updates
criticalLink the new node before you move head or tail, or you lose the old reference.
Wronghead = new_node # old head is lostnew_node.next = head # points at itselfRightnew_node.next = headif head: head.prev = new_nodehead = new_nodeSingle-node list
majorWith one node, head and tail are the same node. Deleting it must clear both.
Wrongif head == tail: head = None # tail still points at the deleted nodeRightif head == tail: head = None tail = NoneTreating it as circular
majorA regular doubly linked list ends at NULL; a circular one loops back to the head.
Wrongwhile current: # fine for regular current = current.next # loops forever if circularRightwhile current is not None: # regular current = current.next# circular: stop when current == headNot clearing deleted node references
minorClear next and prev on a removed node so nothing keeps it alive by accident.
Wrongremoved = currentcurrent = current.next# removed.next / removed.prev still setRightremoved = currentcurrent = current.nextremoved.next = Noneremoved.prev = NoneAlways walking from the head
minorYou can start from either end. Choose the closer one.
Wrongcurrent = headfor _ in range(pos): current = current.nextRightif pos < length // 2: walk from head with nextelse: walk from tail with prevSkipping sentinel nodes
minorDummy head and tail nodes remove most null checks; many production lists use them.
Wrongif 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