Command Palette

Search for a command to run...

Module 8

Linked Lists

Nodes joined by next pointers: walk them safely, add a dummy head, reverse in place, and use fast and slow pointers to find middles and cycles.

Intermediate 4 lessons 9 problems ~50 min of lessons

A linked list trades the array's instant indexing for cheap insertion and removal: changing two pointers splices a node in or out. Almost every linked list problem is about moving pointers in the right order without losing the rest of the list.

Four techniques cover nearly every interview question: careful traversal, the dummy head, in-place reversal, and fast and slow pointers. The module ends with merging k sorted lists, which joins lists with a heap or divide and conquer.

Best after: Two Pointers

Part 1

Learn the ideas

Part 2

Solve the problems

In order of difficulty. Each one shows the pattern it teaches.

  1. The three-pointer reversal you'll reuse in palindrome checks, reordering and group reversal.

  2. Fast and slow pointers find the middle in one pass, without counting the length first.

  3. Floyd's cycle detection: a hash set uses O(n) memory; two runners at different speeds need O(1).

  4. Build a new list behind a dummy head by always taking the smaller front node.

  5. Two pointers with a fixed gap of n find the node before the target in one pass; a dummy head handles removing the first node.

  6. Grade-school addition on linked lists: walk both lists, carry the tens digit, and build the result behind a dummy head.

  7. Combine two techniques: find the middle with fast and slow, reverse the second half, then compare.

  8. Three steps you already know, chained: split at the middle, reverse the second half, weave the halves together.

  9. Scale "merge two" to k lists: a min-heap of the k fronts, or pairwise merging in rounds, both O(N log k).