Step 08 Linked-List — Fundamentals (Doubly LL)
Introduction to Doubly LL
A doubly linked list gives every node two pointers — next and prev — so the chain can be walked in both directions. That one extra pointer removes the singly linked list's biggest weakness: with a node in hand you can now reach its predecessor in O(1), which makes deletions of a given node constant-time and enables backward traversal. The cost is one extra pointer per node and double the link bookkeeping on every update.
Anatomy: node with prev and next
A doubly linked node stores data, next (successor or None at the tail) and prev (predecessor or None at the head). The list is still referenced by head; the head's prev is always None and the tail's next is always None — those two facts are your boundary checks everywhere.
The invariant to protect at all times: for every adjacent pair A -> B, B.prev is A. Any operation that updates a next pointer but forgets the mirror prev pointer leaves the list silently corrupted — forward traversal looks fine while backward traversal breaks. Most DLL bugs are exactly this.
class DNode:
def __init__(self, data, prev=None, next=None):
self.data = data
self.prev = prev
self.next = next
def array_to_dll(arr):
if not arr:
return None
head = DNode(arr[0])
tail = head
for x in arr[1:]:
node = DNode(x, prev=tail)
tail.next = node
tail = node
return headTraversal in both directions
Forward traversal is identical to the singly case: temp from head, follow next. Backward traversal needs the tail first: walk forward to the end once, then follow prev back to the head. Reversing a DLL (a practice problem in this module) exploits exactly this symmetry — swap prev and next on every node, and the old tail becomes the new head.
Because you can move both ways, some problems get cheaper: finding pairs with a given sum in a sorted DLL uses two pointers starting at head and tail moving toward each other, an O(n) trick impossible in a singly list without extra space.
def traverse_backward(head):
if head is None:
return
tail = head
while tail.next is not None:
tail = tail.next
temp = tail
while temp is not None:
print(temp.data, end=' -> ')
temp = temp.prev
print('None')What the extra pointer buys — and costs
Buys: (1) delete a node given only that node in O(1) — its neighbours are reachable through prev and next; (2) insert before a given node in O(1); (3) backward traversal and reverse-order processing; (4) cheaper tail deletion, since the tail's predecessor is tail.prev.
Costs: one extra reference per node (memory), and every splice must update up to four links instead of two, with the mirror-image bug risk described above. Insertion and deletion at a known position are still O(1) rewiring, but reaching position k is O(n) just as before.
Common mistakes
(1) Updating next but not prev (or vice versa) during a splice — always change links in matched pairs. (2) Forgetting head.prev must be set to None when a new head is created or the old head is deleted. (3) In array_to_dll-style construction, creating the node but forgetting to pass prev=tail, which produces a list that only looks doubly linked. (4) Assuming a stored tail pointer exists — unless you maintain one, reaching the tail still costs a full walk.
Key points
- DLL node = data + prev + next; head.prev is None and tail.next is None, always.
- Invariant: if B follows A, then B.prev is A — update links in mirror pairs or the list corrupts silently.
- Given a node, its predecessor is O(1) away, so delete/insert around a known node is O(1).
- Backward traversal and sorted-DLL two-pointer tricks are the practical payoffs.
- Cost: one extra pointer per node and roughly double the bookkeeping per update.