#206

Reverse Linked List

Easy Iterative pointer reversal

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Reverse a singly linked list and return the new head.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: make every arrow point the other way.

Why this works: walk once, saving next before flipping cur.next back to prev.

Example: 1 → 2 → 3

prev null, cur 1: 1 → nullcur 2: 2 → 1cur 3: 3 → 2 → 1, head = 3

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Overwriting cur.next before saving it, which loses the rest of the list.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1) iterative (O(n) stack for the recursive version).
Topic: Linked List
🎬 Episodes 40–45: video coming soon