Notes & software courses · Free to learn
Aph's Blog

LC206 Reverse Linked List 🟢

👋 อ่านฟรีทั้งหมดบน Aph's Blog — เนื้อหาภาษาไทย ทำตามทีละหน้าใน sidebar ได้เลย หากมีข้อเสนอแนะหรืออยากให้เพิ่มหัวข้อไหน บอกได้เสมอ

In-place reverse — flip every next pointer with prev / cur / nxt.

Given the head of a singly linked list, reverse the list, and return the reversed list.

Example 1
Input:
head = [1,2,3,4,5]
Output:
[5,4,3,2,1]
Example 2
Input:
head = [1,2]
Output:
[2,1]
Example 3
Input:
head = []
Output:
[]
Constraints (ข้อจำกัด)
  • The number of nodes in the list is the range [0, 5000].
  • -5000 <= Node.val <= 5000
Full solution · Try yourself firstพับไว้ด้านใน — คลิกเมื่อพร้อมดู

This matches the "reverse the chain" signal — and the mantra: "Before you flip a link, make sure you haven’t stranded the rest of the train!"

1. Mindset Shift

The naive way is to copy every value into a list, reverse it, and build a new linked list — correct, but Space O(n).

Key insight: don’t move the cargo — just walk and flip each next pointer to point backward in place, using prev and cur.

Picture crossing a bridge and folding the planks behind you — you must know where the next step lands before you fold!

2. The Logic — 4 Steps

Open prev/cur, then flip one node at a time:

  1. Prep pointers — prev = None · cur = head
  2. Save the path — nxt = cur.next (don’t lose the train!)
  3. Flip the link — cur.next = prev, then advance prev to cur · cur to nxt
  4. Done — when cur is None, prev is the new head → return prev

3. LeetCode-Ready Code

Memorize the four lines in the loop — this move shows up in many problems:

Submit this on LeetCodepython
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        prev = None                 # behind cur (nothing yet)
        cur = head                  # node under consideration

        while cur:
            nxt = cur.next          # 1) save next (don’t lose the train)
            cur.next = prev         # 2) flip the link backward
            prev = cur              # 3) advance prev
            cur = nxt               # 4) advance cur to the saved node

        return prev                 # after the loop, prev is the new head

4. Dry Run — 1 → 2 → 3

Stepcur (val)nxt (saved)after flip cur.next →prev after steplist now
Before loop1None1 → 2 → 3
Round 112None1None ← 1 | 2 → 3
Round 22312None ← 1 ← 2 | 3
Round 33None23None ← 1 ← 2 ← 3
After loopNone → stop3 = new head3 → 2 → 1

The | mark splits two pieces: left = already flipped · right = not touched yet — final train [3, 2, 1]

5. Edge Cases & Pitfalls

The "forgot to save nxt first" case — classic trap:

  • The moment you set cur.next = prev, the old forward link is gone
  • You can’t walk onward — always do nxt = cur.next first
What do you return?

Return prev, not cur — at the end cur is None (past the end), while prev sits on the last flipped node, which is the new head.

6. Time & Space Complexity

  • Time O(n) — one pass through every node
  • Space O(1) — a few pointers, no new list
💡 Pattern summary

The four-line prev/cur/nxt reverse is the standard move to memorize — problem 32 reuses it to reverse only part of the list.