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)Space O(1)

2 · Watch

Linked List in 60 seconds

Five nodes, each pointing at the next. You could copy the values into a new, backwards list, but that costs a new node for every old one.

3 · Now you drive

Keys:←→ stepSpace playR resetHomeEnd jump

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
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;
}
▶ marks the line running now

4 · Your turn

0 of 6 problems solved

Start from this

// Fake node in front, so the head isn't special.
const dummy = new ListNode(0, head);
let prev = dummy, curr = head;
while (curr) {
  // Save this before rewiring cuts the link.
  const next = curr.next;
  rewire(prev, curr);
  prev = curr;
  curr = next;
}
// The real head, even if the old one moved.
return dummy.next;

Easy ways to go wrong

  • Cutting a link before saving the node after it.
  • Skipping the dummy node, then special-casing the head.
  • Reading .next on a node that might be null.
  1. Easy01

    Reverse Linked List

    LeetCode 206 · Blind 75 · NeetCode 150

    The one you just watched. Write it from memory.

  2. Easy02

    Merge Two Sorted Lists

    LeetCode 21 · Blind 75 · NeetCode 150

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

  3. Easy03

    Linked List Cycle

    LeetCode 141 · Blind 75 · NeetCode 150

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

  4. Medium04

    Reorder List

    LeetCode 143 · Blind 75 · NeetCode 150

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

  5. Medium05

    Remove Nth Node From End of List

    LeetCode 19 · Blind 75 · NeetCode 150

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

  6. Hard06

    Merge k Sorted Lists

    LeetCode 23 · Blind 75 · NeetCode 150

    Merge pairs again and again until one list is left.

UP NEXT · LESSON 8 OF 20DesignPair simple structures so every call is fast.