Sub topic 2
Adding a prev pointer: the doubly linked node
This chapter assumes you know Python classes, how a singly linked list stores a next reference in each node, Big-O notation, and how None works as "no object". Here you will build the node and list structure that the rest of the tutorial uses.
What a singly linked list cannot do well
A singly linked list node knows only its successor. That causes two limits:
- You can only walk forward. Reaching the node before a given node means starting at the head and scanning.
- Removing a node you already hold costs O(n). You must find its predecessor so you can change the predecessor's
next.
A doubly linked list fixes both by storing one extra reference per node.
The doubly linked node
A doubly linked node holds a value, a next reference to its successor, and a prev reference to its predecessor.
class Node:
def __init__(self, value):
self.value = value
self.prev = None
self.next = NoneThe first node has no predecessor, so its prev is None. The last node has no successor, so its next is None. These two None values mark the ends of the list.
The list object
The list object keeps a head and a tail reference, plus a size counter.
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
self.size = 0The tail reference matters because the back pointers give you a way to walk in reverse. With tail, you can start at the last node and go backward. You can also reach either end in O(1).
An empty list has head and tail both set to None and size equal to 0. A one-element list has head and tail pointing to the same node, and that node's prev and next are both None. Most pointer bugs come from forgetting one of these two cases.
Wiring nodes by hand
Before writing insertion methods, wire three nodes manually. This shows what every later operation must maintain.
class Node:
def __init__(self, value):
self.value = value
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
self.size = 0
lst = DoublyLinkedList()
a, b, c = Node(1), Node(2), Node(3)
a.next = b
b.prev = a
b.next = c
c.prev = b
lst.head = a
lst.tail = c
lst.size = 3
print(lst.head.next.prev is lst.head) # True
print(lst.tail.prev.value) # 2
print(a.prev, c.next) # None NoneEvery link takes two assignments, one for each direction. Setting a.next = b without b.prev = a leaves the list in a broken state. Forward traversal would work, but backward traversal would skip or crash.
The core invariant
The structure is consistent when these statements hold:
- For every node
nwithn.nextnotNone,n.next.prev is n. - For every node
nwithn.prevnotNone,n.prev.next is n. head.previsNoneandtail.nextisNone.- If the list is empty,
headandtailare bothNone. Otherwise neither isNone.
Every operation you write in the next chapters must leave these statements true. The invariant also gives you a way to test your code, which a later chapter covers.
The cost of the extra pointer
Each node now stores two references instead of one. In CPython, a plain object with a __dict__ already carries large overhead, so the extra attribute adds a modest amount. In languages with compact nodes, such as C, it can add 8 bytes per node on a 64-bit machine. In return you get reverse traversal, and you can remove a known node in O(1).
| Variant | References per node | Walk backward | Remove a held node |
|---|---|---|---|
| Singly linked | 1 | No | O(n) |
| Doubly linked | 2 | Yes | O(1) |
Exercise
Exercise
In a correctly built doubly linked list with at least two nodes, what does lst.head.next.prev evaluate to, and why?
Show answer
It evaluates to lst.head itself. The invariant says that for any node n with a successor, n.next.prev is n. Following next and then prev returns to the starting node.
Exercise
A list holds exactly one node. What must be true of head, tail, and that node's prev and next?
Show answer
head and tail both refer to the same node. The node's prev and next are both None, and size is 1.