Linked Lists
A linked list stores values in separate nodes, each holding one value and a next pointer to the following node. Insertion at the head costs O(1) time (two pointer writes), while reaching any later position requires walking the chain from head, so access, search, and arbitrary insertion all cost O(n) time.
A linked list works like a scavenger hunt where each clue holds one value and tells you where the next clue is hidden, so reaching clue five requires following clues one through four first. Inserting a new clue in the middle only requires updating two pointers without moving any downstream nodes.
How a Linked List Works
The displayed code is a singly linked list: a Node holding value and next, and a LinkedList holding one entry point, head. Because there is no tail pointer, insert_tail walks the whole chain on every call, and the complexity table reflects that O(n) cost.
Every insertion rewires two pointers. insert_head(v) points the new node's next at the old head, then updates head to the new node (two writes, zero walking). insert_at(k, v) walks to position k − 1, then performs the same two writes: newNode.next = curr.next, followed by curr.next = newNode. Because reversing that order drops the rest of the list (see Common Mistakes), the animation plays the writes as separate beats.
delete(v) checks the head first as an O(1) special case, since the head has no predecessor to rewire. Otherwise curr walks while looking ahead: it probes curr.next.value rather than curr.value, so when the target matches, curr already stands on the predecessor where curr.next = curr.next.next needs it. search(v) is a direct walk: compare each node, advance, and return the index or -1.
The capacity slider (4–10 nodes, default 7) is a canvas layout limit mirrored by a max field in the displayed code. A real linked list has no fixed capacity and grows dynamically as nodes are allocated.
Linked List Operations Step by Step
The list starts empty. Run this sequence from the Inputs panel:
- Insert Head 5. The new node stages above the row, its
nextaims at the sentinelnull(the old empty head), andheadswings onto it. List:5 → null. - Insert Head 8. Node 8 stages above the row,
8.nextaims at the current head 5, andheadredirects to 8. List:8 → 5 → null. - Insert Tail 3.
currwalks index 0 (value 8) and index 1 (value 5, whosenextisnull, stopping the walk). Then3.next = nulland5.nextswings to 3. List:8 → 5 → 3 → null. - Insert At Index 1, value 7.
currwalks to the predecessor at index 0. Then7.next = 8.next(node 5), followed by8.next = 7. List:8 → 7 → 5 → 3 → null. - Search 5. Probe index 0 (
8 ≠ 5), index 1 (7 ≠ 5), and index 2 (5 = 5). Found at index 2. - Delete 5. Head check:
8 ≠ 5. Looking ahead,currat index 0 probes7 ≠ 5and at index 1 probes5 = 5. Node 5 lifts out,7.nextbypasses it to 3, and the chain closes. List:8 → 7 → 3 → null.
On the canvas, the curr cursor marks each probe, a staged node hovers above its slot until both pointer writes complete, and a deleted node lifts out while the bypass arrow swings past it.
Time and Space Complexity
| Operation | Cost | Why |
|---|---|---|
| Access by index | O(n) | follow clues from head |
| Insert at head | O(1) | two pointer writes, no walk |
| Insert at tail | O(n) | no tail pointer here; walks the chain |
| Insert at index k | O(k) | walk to the predecessor, then O(1) splice |
| Delete by value | O(n) | look-ahead walk to the predecessor |
| Search | O(n) | compare node by node |
| Space | O(n) | one node plus one pointer per value |
A tail pointer would make tail insertion O(1); the code omits it to keep the traversal visible. The splice itself always takes O(1) time once traversal reaches the predecessor.
Edge Cases
- Insert into an empty list. Head and tail insertion perform the same move:
headswings fromnullonto the new node. - Delete or search on an empty list. Both return without raising an exception:
deletehits itsif not self.headguard (False), andsearchskipswhile curr(-1). - Delete the head. Handled by the O(1) head check (
head = head.next), with no traversal. - Delete a missing value.
curr.nextreachesnulland the guard returnsFalse. insert_at(k, v)withkpast the end. The code has no bounds check and would walk off the end of the chain; the visualization stops and reports the out-of-bounds index instead. (k = 0delegates directly toinsert_head.)
Common Mistakes
- Splicing in the wrong order. Write
newNode.next = curr.nextbeforecurr.next = newNode. Reversing the two lines overwrites the only pointer to the rest of the list and drops every remaining node. - Stopping on the target node instead of its predecessor. Rewiring needs the node before the target, which is why the delete walk probes
curr.nextrather thancurr. - Off-by-one in the walk condition.
while curr.nextstops on the last node (what tail insertion wants);while currinspects the last node too (what search wants). Swap them and you skip the final node or dereferencenull. - Using a linked list when an array fits better. Following pointers through scattered memory causes frequent cache misses. Pick a linked list when you splice frequently in the middle and rarely access by index; otherwise a contiguous array usually performs better.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Embed this lesson in a blog, docs, or course?
Found a mistake or something unclear?