Unlink changes two pointers, and push back changes four
Every LRU operation comes down to two moves on that list.
Predict
Push back sets four pointers. Which one must you read before you overwrite it?
An LRU cache needs two moves, and a get does both. Here they are as a moves to the newest end:
Moving a to the newest end
list
beforeheadabtailunlink aheadbtailpush a backheadbatail
Step
The pointers it sets
unlink a
head.next = b, b.prev = head
push a back
b.next = a, a.prev = b, a.next = tail, tail.prev = a
Unlink makes a node's two neighbours point at each other. a's own pointers are left stale, which is harmless: pushing it back overwrites them.
Push back puts a node just before tail, so it becomes the newest. Read tail.prev, the old newest node, before you overwrite it.
In the editor, write those two moves as functions: unlink(node) and push_back(tail, node).
Pick an answer above to see the rest
Instructions
1
Write unlink(node), which takes node out of the list by joining its two neighbours, and push_back(tail, node), which puts node just before the tail sentinel. No loops: each one is a few pointer assignments.