Skip to content

Start typing to search patterns, problems and pages.

Linked List

A treasure hunt where each clue says where the next one hides. To run it backwards you rewrite one clue at a time, keeping a finger on the next clue before you overwrite it. Reversing a list is the same: save next, flip, step on.

⏱ about 15 minutesNeeds: pointers, referencesTime O(n)

2 · Watch

No sound? Captions are on by default.

3 · Now you drive

Keys:←→ stepSpace playR reset

Reverse Linked List

or try
1–7 numbers from -99 to 99, separated by spaces.
prev starts at nothing, curr at the head, 1.Step 1 of 17. Starting out. List: 1, 2, 3, 4, 5. prev at nothing, curr at 1.
Step 1 of 17
solution.ts▶ marks the line running now
function reverseList(head: ListNode | null): ListNode | null {
let prev: ListNode | null = null;
let curr = head;
while (curr !== null) {
const next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}

4 · Your turn

0 of 6 problems solved
  1. Easy01

    Reverse Linked List

    The one you just watched. Write it from memory.

  2. Easy02

    Merge Two Sorted Lists

    Warm-up: walk both lists, always taking the smaller head.

  3. Easy03

    Linked List Cycle

    A fast pointer laps a slow one exactly when a loop exists.

  4. Medium04

    Reorder List

    Find the middle, reverse the back half, then weave.

  5. Medium05

    Remove Nth Node From End of List

    Two pointers, n apart, walk to the end together.

  6. Hard06

    Merge K Sorted Lists

    Merge pairs again and again until one list is left.

UP NEXT · LESSON 7 OF 15TreesRecursion that follows the branches.