← Pointer Walk

Pattern Recognition Drill

#9 — Insert at position k

Easy Linked Lists

The Problem

Insert value val at position k (0-indexed) in a linked list.

What approach would you use?

Think about it before scrolling down.

Key Signals

Pointer Walk

Walk to node k-1, set new_node.next = node.next, node.next = new_node. O(k) time.

Common Trap

Don't forget the edge case: inserting at position 0 (head) needs special handling or a dummy node.

← #8 #10 →