Best on a larger screen — the animation and controls have more room to breathe.
Inputs

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:

  1. Insert Head 5. The new node stages above the row, its next aims at the sentinel null (the old empty head), and head swings onto it. List: 5 → null.
  2. Insert Head 8. Node 8 stages above the row, 8.next aims at the current head 5, and head redirects to 8. List: 8 → 5 → null.
  3. Insert Tail 3. curr walks index 0 (value 8) and index 1 (value 5, whose next is null, stopping the walk). Then 3.next = null and 5.next swings to 3. List: 8 → 5 → 3 → null.
  4. Insert At Index 1, value 7. curr walks to the predecessor at index 0. Then 7.next = 8.next (node 5), followed by 8.next = 7. List: 8 → 7 → 5 → 3 → null.
  5. Search 5. Probe index 0 (8 ≠ 5), index 1 (7 ≠ 5), and index 2 (5 = 5). Found at index 2.
  6. Delete 5. Head check: 8 ≠ 5. Looking ahead, curr at index 0 probes 7 ≠ 5 and at index 1 probes 5 = 5. Node 5 lifts out, 7.next bypasses 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

OperationCostWhy
Access by indexO(n)follow clues from head
Insert at headO(1)two pointer writes, no walk
Insert at tailO(n)no tail pointer here; walks the chain
Insert at index kO(k)walk to the predecessor, then O(1) splice
Delete by valueO(n)look-ahead walk to the predecessor
SearchO(n)compare node by node
SpaceO(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: head swings from null onto the new node.
  • Delete or search on an empty list. Both return without raising an exception: delete hits its if not self.head guard (False), and search skips while curr (-1).
  • Delete the head. Handled by the O(1) head check (head = head.next), with no traversal.
  • Delete a missing value. curr.next reaches null and the guard returns False.
  • insert_at(k, v) with k past 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 = 0 delegates directly to insert_head.)

Common Mistakes

  • Splicing in the wrong order. Write newNode.next = curr.next before curr.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.next rather than curr.
  • Off-by-one in the walk condition. while curr.next stops on the last node (what tail insertion wants); while curr inspects the last node too (what search wants). Swap them and you skip the final node or dereference null.
  • 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.