Sub topic 5
Removing a node in O(1) when you hold a reference to it
This chapter assumes you know Python classes and generators, how a singly linked list stores a next reference, Big-O notation, and how None behaves as a reference. It builds on the Node and DoublyLinkedList classes from the earlier chapters, which have head, tail, size, push_front, push_back and insert_after.
Why a singly linked list cannot do this
To remove a node from a singly linked list, you must change the next pointer of its predecessor. A node only knows its successor. So even when you hold the node itself, you have to walk from the head to find the one before it. That walk costs O(n).
A doubly linked node stores prev. The predecessor is one attribute access away, so no search is needed.
The removal operation
Removing a node means making its neighbors point at each other. There are three cases, depending on whether the node has a predecessor and a successor.
def remove(self, node):
if node.prev is not None:
node.prev.next = node.next
else:
self.head = node.next
if node.next is not None:
node.next.prev = node.prev
else:
self.tail = node.prev
node.prev = None
node.next = None
self.size -= 1Add this method to DoublyLinkedList. It does the following:
- If the node has a predecessor, that predecessor's
nextskips over the node. If not, the node was the head, soheadmoves to the node's successor. - If the node has a successor, that successor's
prevskips back over the node. If not, the node was the tail, sotailmoves to the node's predecessor. - The node's own pointers are cleared, and
sizedrops by one.
The method uses a fixed number of assignments and no loop, so it runs in O(1).
The edge cases
Each branch in remove matches one position:
| Position of node | `prev` | `next` | What changes |
|---|---|---|---|
| Middle | not `None` | not `None` | Both neighbors are relinked |
| Head | `None` | not `None` | `head` moves forward, successor's `prev` becomes `None` |
| Tail | not `None` | `None` | `tail` moves back, predecessor's `next` becomes `None` |
| Only node | `None` | `None` | `head` and `tail` both become `None` |
The only-node case needs no extra code. The first else sets head to None, and the second sets tail to None. Forgetting one of these is a common bug: the list looks empty from the front but tail still points at a removed node.
Why clear the removed node's pointers
Setting node.prev and node.next to None has two purposes. It stops a stale node from keeping its old neighbors reachable, which would delay garbage collection. It also makes misuse easier to notice. If you call remove twice on the same node, the second call sees prev and next both None and will wrongly reset head and tail to None, corrupting the list.
Trying it
lst = DoublyLinkedList()
a = lst.push_back("A")
b = lst.push_back("B")
c = lst.push_back("C")
lst.remove(b)
print(list(lst)) # ['A', 'C']
print(list(lst.reversed())) # ['C', 'A']
lst.remove(a)
lst.remove(c)
print(list(lst), lst.head, lst.tail, lst.size) # [] None None 0Checking both directions is a useful habit. If next links are right but prev links are wrong, forward traversal looks correct while reversed() shows the damage.
The catch: finding the node
The O(1) cost applies only when you already hold the node. If you have a value and must locate its node first, you pay O(n) for the search, and removal adds only a constant on top. Structures such as an LRU cache avoid this by pairing the list with a dictionary that maps each key to its node, so lookup and removal are both O(1).
Exercise
A doubly linked list holds one node, X. You call remove(X). Which fields of the list change, and what are their final values?
Show answer
head becomes None because X.prev is None, so the else branch sets head to X.next, which is None. tail becomes None because X.next is None, so the else branch sets tail to X.prev, which is None. size goes from 1 to 0. X.prev and X.next are set to None.
Exercise
Why does removing a node from a singly linked list take O(n) even when you hold the node, while a doubly linked list takes O(1)?
Show answer
The singly linked list must update the predecessor's next pointer, and without a prev pointer it has to walk from the head to find the predecessor. The doubly linked node reaches its predecessor directly through prev, so removal needs only a fixed number of pointer assignments.