Sub topic 7
Sentinel nodes remove the edge cases
This chapter assumes you know basic Python classes, how object references work, and Big-O notation. It also assumes you have seen why an LRU cache pairs a dictionary with a doubly linked list. Here you will see why the list uses two placeholder nodes, called sentinels, and what they save you from.
The problem: pointer updates that depend on position
Without sentinels, the list keeps head and tail references that are None when the list is empty. Every operation then has to ask where the node sits.
- Adding to an empty list must set both
headandtail. - Removing the only node must reset both to
None. - Removing the first node must update
head, and removing the last node must updatetail. - Removing a middle node updates only its neighbors.
Each case is a separate branch, and each branch is a place for a bug. A missed None check raises AttributeError, and a forgotten tail update silently corrupts the order.
The idea: a sentinel is a node that is never real data
A sentinel node is a dummy node that marks the boundary of the list. It never holds a user's key or value, and it is never removed. The list creates two of them:
head, a sentinel at the front.tail, a sentinel at the back.
Real nodes always live between them. An empty list is just head linked to tail. Because every real node has a real or sentinel neighbor on both sides, node.prev and node.next are never None for a real node.
How the code uses them
The constructor from the previous design builds the empty state directly:
class Node:
__slots__ = ("key", "value", "prev", "next")
def __init__(self, key=None, value=None):
self.key = key
self.value = value
self.prev = None
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = Node() # sentinel
self.tail = Node() # sentinel
self.head.next = self.tail
self.tail.prev = self.head
def add_front(self, node):
node.prev = self.head
node.next = self.head.next
self.head.next.prev = node
self.head.next = node
def remove(self, node):
node.prev.next = node.next
node.next.prev = node.prevLook at remove. It has two lines and no if. It works the same for the first node, the last node, and a middle node, because node.prev and node.next always exist. add_front is also branch-free. When the list is empty, self.head.next is the tail sentinel, so the line self.head.next.prev = node sets tail.prev correctly.
Common errors
- Storing data in a sentinel. The sentinels use
key=None. If you put real data there, eviction could remove the wrong thing. - Removing a sentinel. Never call
remove(self.head)orremove(self.tail). Both would break the links for the whole list. - Treating the tail sentinel as the oldest entry. The least recent real node is
tail.prev. Iftail.prev is head, the list is empty, andpop_backmust check for this before it removes anything.
Exercise
In a list with sentinels that holds one real node X, which four references change when you call remove(X), and what does the list look like afterward?
Show answer
Only two references change: head.next is set to X.next, which is tail, and tail.prev is set to X.prev, which is head. Afterward head and tail point at each other, which is the empty list. No special case for "only one node" was needed.