2Brute2Best
🔥0

Reverse Linked List

Easy·LeetCode #206Basic Anchor
LeetCode
30-Second Mental ModelLinked List — Reversal (full reversal, three pointers)
Complexity:O(N)→O(N)
Key Insight

Each node needs its arrow to point at the node before it. Walk the list once. Keep prev (the reversed part) and cur (the next node to flip). Save cur->next before each flip, so the rest of the list is not lost.

Core Validity Invariant

The list is reversed when every node's next points to the node before it in the original list, and the old head points to null.

01

Problem

You are given the head of a singly linked list. Reverse the list, so the last node becomes the first and every arrow points the other way. Return the new head.

Input

head, the first node of a singly linked list. The list can be empty.

Output

The head of the reversed list.

Example 1
Input: head = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]

Every arrow now points back towards the start.

Example 2
Input: head = [1, 2]
Output: [2, 1]

Only the one arrow between them flips. Node 2 now points to node 1, and node 1 points to null.

Example 3
Input: head = []
Output: []

An empty list has nothing to reverse.

Constraints
  • • The list has between 0 and 5000 nodes.
  • • -5000 ≤ Node.val ≤ 5000
  • • Follow-up: can you reverse the list both iteratively and recursively?
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.

  1. 1.

    If I walk the list from the head, which arrow can I flip first? What do I lose when I flip it?

  2. 2.

    After I flip the first arrow, can I still reach the second node? How?

  3. 3.

    After the reversal, what should the old first node point to?

  4. 4.

    How do I know when to stop, and which pointer holds the answer at that moment?

  5. 5.

    Could I swap values from both ends, like an array? What stops me?

03

Brute force approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForcePush every node on a stack, then pop them back in reverse orderO(N)O(N)
OptimalWalk once, flip each arrow back, keep prev and curO(N)O(1)
09

Why does this belong to this pattern?

Clues in the problem statement
  • “reverse the list”Every arrow must flip. A linked list can't walk backwards, so we flip each arrow while walking forward.
  • “singly linked list”Each node only knows its next node. Save the next node before you change an arrow.
  • “return the new head”The old head becomes the last node. The new head is the last node we reach, which is the one prev holds.
walk forward once+flip one arrow per node+prev, cur, nxt→Linked List — Reversal (full reversal, three pointers)

This is the base of the whole reversal pattern: walk forward once and flip each arrow. Later questions reverse part of a list, or use this same move on a doubly linked list or in a number. The one rule to keep: before you change cur->next, save it. Each later question in this sub-pattern changes this basic move a little.

10

Don't make this mistake

Flipping the arrow before saving the rest of the list
✗ while (cur != nullptr) { cur->next = prev; prev = cur; cur = cur->next; // cur->next is now prev, so we jump backwards }
✓ while (cur != nullptr) { ListNode* nxt = cur->next; // save first cur->next = prev; prev = cur; cur = nxt; }

After cur->next = prev, the arrow no longer points to the rest of the list. The last line reads cur->next and gets the old prev, which is null on the first node. So cur becomes null, the loop stops after one node, and the rest of the list is lost.

Pattern confusion: swapping values from both ends, like an array
✗ ListNode* left = head; ListNode* right = lastNode(head); // a full walk to find the end while (left != right) { swap(left->val, right->val); left = left->next; // right would need to step backwards, which a singly linked list can't do }
✓ Flip the arrows with three pointers in one pass from the head.

Reversing an array by swapping from both ends works because you can jump straight to index n − 1 − i. A singly linked list can't step backwards, so right can't move inward. On [1, 2, 3, 4, 5], the first swap trades 1 and 5. The next pair is 2 and 4, and node 4 sits just before the tail, so it can only be found again with a walk from the head. Every pair repeats that walk, which makes the whole thing O(N²).

Returning the old head
✗ while (cur != nullptr) { ListNode* nxt = cur->next; cur->next = prev; prev = cur; cur = nxt; } return head; // head is now the last node
✓ return prev; // the old tail is the new head

The old head was the first node, so its next is null after the reversal. Returning it gives back a list with one node. The new head is the last node we flipped, and prev holds it.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Linked List — Reversal translate into production architectures and low-level memory mechanics.