Doubly linked lists and sentinels
Link nodes both ways with prev and next, use a header and a trailer sentinel so that insert and remove need no case for the ends, and say which operations cost O(1) here and which cost O(n) compared with a Python list.
It opens on
Every node here holds links to both its neighbours, and we are handed only the node 8. Can we unlink it in a few steps, or must we first walk along the list?
Why it matters
A browser's tab strip, a text editor's lines and the least recently used list inside a cache all keep their items in a doubly linked list, because removing an item you already hold, from anywhere, takes a few assignments and no walk.
Good to know first
- Linked list insertion and deletion: new.next is set before node.next, and removing the last node needs a walk from the head
- Singly linked lists: a Node holds data and next, and the last next is None
- Dynamic arrays and amortized cost: list.append is O(1) amortized and inserting at the front of a list is O(n)
Step by step
Two links per node
Read a doubly linked list, keep next and prev agreeing, and remove a given node in O(1) by rewriting two fields.
Line 2 sets the next of node.prev, the node 3, to node.next, the node 5. Line 3 sets the prev of node.next, the node 5, to node.prev, the node 3.
A common mix-up
Updating only one direction is enough, since forward reading still works.
In fact: Both links of a pair must be rewritten, or reading backward from the 5 would still reach the removed 8.
Sentinel nodes
Build a list with a header and a trailer sentinel, write insert_between, and see that insert and remove then have no case for the ends or the empty list.
Take the empty list and call add_last(8). It calls _insert_between with pred = trailer.prev, which is the header, and succ = the trailer. The new node gets prev header and next trailer.
A common mix-up
The sentinels are items of the list, so size counts them.
In fact: They hold no data and are not counted, which is why the empty list has size 0 although two nodes exist.
The ends and before a node
Insert before a given node and remove from either end or at a given node, by choosing the two nodes, and trace the list forward and backward after each operation.
Start with 3, 8, 5 and hold the node 8. insert_before(node, 6) calls _insert_between with pred the node 3 and succ the node 8, so the list reads 3, 6, 8, 5 forward and 5, 8, 6, 3 backward.
the first two rows of the table
A common mix-up
Insert before a node is O(n) in any linked list, since you must find the node before.
In fact: With prev the node before is node.prev, so insert before a held node is O(1) here.
Against the array
Compare a doubly linked list with a Python list operation by operation, and choose the structure for a given workload.
With 1000 items, get(10) starts after the header and makes 10 hops. get(990) is not below 500, so it starts at the trailer and makes 999 − 990 = 9 hops. get(500) makes 499 hops.
A common mix-up
A linked list is faster than a list for everything that changes the sequence.
In fact: It is faster only at the front and at a node already held; reading by index and finding a value are no better, and at the end the two tie.
Try it
For the list 2, 6, 4 with no sentinels, the node 6 is removed. What are n2.next.data and n4.prev.data afterwards?Answer
4 and 2
Hint: n2 is the node before 6, and n4 the node after
Starting with an empty sentinel list, add_last(4), add_first(9), add_last(2). What does it read forward, and what is trailer.prev.data?Answer
9, 4, 2 forward, and trailer.prev.data is 2
Hint: add_first goes right after the header
Start with 4, 7 in a sentinel list. After add_last(9), insert_before(the node 7, 5) and remove_first(), what does the list read forward?Answer
5, 7, 9
Hint: The list goes 4, 7, 9, then 4, 5, 7, 9, then 5, 7, 9
A list of 2000 items: how many items does list.pop(0) shift, and how many hops does get(1500) make on the linked list?Answer
1999 items shifted, and 499 hops
Hint: The nearer end of index 1500 is the trailer, 2000 − 1 − 1500 hops
Which two fields were rewritten, and in which nodes?Answer
The next field of the node 3 and the prev field of the node 5; the fields of the node 8 itself are not touched.
