Linked Lists
A linked node carries a value and a reference to its successor. Positions come from following links from the head.
Picture the idea
Each clue in a treasure trail gives the location of the next clue. Redirecting one clue changes the route even when every clue stays physically in place.
When to use it
Use linked nodes for local rewiring when you already hold the relevant references.
How it works
To reverse a chain, save current.next, aim current.next at the previous node, then advance using the saved reference.
- Walk a chain until null.
- Splice a node after a known node.
- Reverse a link while preserving the unprocessed tail.
- Advance a fast cursor twice.
An indexed lookup is O(n). Insertion after a known node is O(1). Iterative reversal takes O(n) time and O(1) auxiliary space.
Watch for this mistake
Overwriting next before saving it disconnects the remainder of the chain.
Draw references for an empty list, a one-node list and a two-node list.