The idea
remove_node on the node for a, beside b, returns a and leaves b.
A doubly linked node stores prev and next. If you already hold the node, you do not walk from the head to find its neighbors. remove_node on the node for a, in a list that also holds b, returns a and leaves b. The unlink follows prev and next and rewires those neighbors to each other. insert_after splices a new node between a known node and that node's next. Both directions must be written. A missing reverse link makes a forward walk and a backward walk disagree. Finding a node by value is still linear, unless another structure stores the node. Sentinels are one way to make every real node have neighbors, including the empty list. This list keeps a head and a tail instead, and the endpoint cases update those caches.
A worked trace
| Step | State | What changed |
|---|---|---|
| Append | head = a | Append a. Head and tail both hold a. |
| Append | tail = b | Append b. The tail is now b. |
| Unlink | removed = a | Removing the known node a leaves b. |
Why it is correct
The known-node claim is that the predecessor and the successor are fields, not search results. Rewiring them restores a single chain. The forward neighbor's prev and the backward neighbor's next must both change, or one direction still points at the removed node. insert_after writes the new node's two links and the two neighboring links that should point at it. That is four pointers when both neighbors exist. An endpoint has a missing neighbor, so the list updates head or tail instead of a second node. The size cache changes by one. After the edit, a walk from the head and a walk from the tail must list the same nodes in opposite orders.
A common mistake
Writing only the new node's links leaves the old neighbors pointing at each other.
What it costs
Unlinking or inserting beside a known node is a constant number of pointer writes. It is constant time. A singly linked delete of an arbitrary node is linear, because the predecessor must be found from the head. A search by value on this list is still linear. n such searches are quadratic if you repeat them. Extra memory is one node per item plus the list caches. Draw both directions after a removal. Test an empty list and a one-node list, and check that head and tail still agree. A known-node unlink is Θ(1) time and O(1) extra memory.
Run it
from bitsandbytes.doubly_linked_list import DoublyLinkedListchain = DoublyLinkedList()first = chain.append('a')chain.append('b')print(chain.remove_node(first))# prints: a Practice and next step
Open Stack basics next. Stack basics is the same idea of a single end, with the opposite arrival order.