Compile Ready
Module 2 · Basic Operations

Reverse Linked List

EasyProblem 1 of 17 8 min read ~15 min to solve LeetCode
Linked ListPointer ReversalThree PointersIterationRecursion
Asked atMicrosoftAmazonGoogleMetaApple

Problem Statement

Given the head of a singly linked list, reverse the list and return the new head. The original links must be rewired so that 1 -> 2 -> 3 -> null becomes 3 -> 2 -> 1 -> null.

Input

The head pointer of a singly linked list, or null for an empty list.

Output

The head pointer of the reversed linked list.

Constraints

  • 0 <= number of nodes <= 5000
  • -5000 <= Node.val <= 5000

Examples

Example 1

Input:
head = **1 -> 2 -> 3 -> 4 -> 5 -> null**
Output: **5 -> 4 -> 3 -> 2 -> 1 -> null**
Explanation: Every next pointer is redirected to the previous node, so the tail **5** becomes the new head.

Example 2

Input:
head = **1 -> 2 -> null**
Output: **2 -> 1 -> null**
Explanation: The link **1 -> 2** is flipped into **2 -> 1**, and **1.next** becomes **null**.

Example 3

Input:
head = **null**
Output: **null**
Explanation: An empty list has no pointers to reverse, so the answer is still **null**.

Learning Objectives

  • Master the foundational **prev**, **curr**, and **next** pointer reversal move.
  • Explain why saving the next node before rewiring prevents losing the rest of the list.
  • Reason about the reversed prefix and unreversed suffix invariant during a linked-list traversal.
  • Compare iterative reversal with the recursive call-stack version.

Intuition

Pattern Recognition

The signal is any prompt that says reverse a linked list or asks you to flip node order in place. Arrays can swap values by index, but a singly linked list only moves forward, so the real task is pointer surgery: each node must stop pointing to its next node and start pointing to the node before it.

The pointer trap is losing access to the remaining suffix. If curr points at 2 in 1 -> 2 -> 3, and you immediately set curr.next = prev, the old link to 3 is gone unless you saved it first. The safe template is always: save next, reverse curr.next, advance prev, advance curr.

Common mistakes

  • ×Reassigning **curr.next** before saving the old next node, which disconnects the rest of the list.
  • ×Returning the original **head** instead of **prev**, even though the old head becomes the new tail.
  • ×Forgetting to set the old head next pointer to **null**, which can create a cycle.
  • ×Advancing **curr** before moving **prev** to the node that was just reversed.

Algorithm Explanation

Key idea

Maintain two regions: a reversed prefix ending at prev, and an unreversed suffix beginning at curr. At each step, move curr from the front of the suffix to the front of the reversed prefix. When curr becomes null, prev is the new head.

Pointer walkthrough

Start with 1 -> 2 -> 3 -> null. Initially prev = null and curr = 1. Save next = 2, then point 1.next back to null. Now the reversed prefix is 1 -> null, and the remaining suffix is 2 -> 3 -> null. Advance prev to 1 and curr to 2.

At curr = 2, save next = 3 before touching links. Point 2.next to 1, producing the prefix 2 -> 1 -> null, while next still remembers the suffix head 3. Advance again. At curr = 3, save next = null, point 3.next to 2, and the prefix becomes 3 -> 2 -> 1 -> null. The suffix is empty, so prev = 3 is returned.

Algorithm

  1. Set prev = null and curr = head.
  2. While curr is not null, save next = curr.next.
  3. Reverse the current link by setting curr.next = prev.
  4. Move prev forward to curr.
  5. Move curr forward to the saved next node.
  6. Return prev as the head of the reversed list.

Solutions

Solution 1: Iterative three-pointer reversal

When to prefer this:

Use this in interviews by default. It is iterative, constant-space, and exposes the exact pointer invariant most linked-list problems reuse.

Sweep through the list once with prev, curr, and a saved next pointer. Each iteration removes curr from the unreversed suffix and prepends it to the reversed prefix.

Step-by-step

  1. Initialise prev to null and curr to head.
  2. Save curr.next in nextNode before changing any links.
  3. Point curr.next backward to prev.
  4. Advance prev to curr and curr to nextNode.
  5. When the traversal finishes, return prev because it points at the old tail, now the new head.
Time

O(n)

Space

O(1)

Each node is visited once and only three pointers are stored.

Java implementation

Loading…

Solution 2: Recursive reversal

When to prefer this:

Use this when the interviewer asks for a recursive formulation or wants to discuss how the call stack reverses the suffix first. Prefer the iterative version when stack depth matters.

Recursively reverse the suffix starting at head.next, then attach head after that reversed suffix. The base case is an empty list or a single node, which is already reversed.

Step-by-step

  1. If head is null or head.next is null, return head.
  2. Recursively reverse the list starting at head.next and keep the returned newHead.
  3. The node after head is now the tail of the reversed suffix, so set head.next.next = head.
  4. Set head.next = null so the old head becomes the new tail.
  5. Return newHead unchanged through the stack.
Time

O(n)

Space

O(n)

The same nodes are rewired once, but recursion uses one stack frame per node.

Java implementation

Loading…

Dry Run

Sample input

head = 1 -> 2 -> 3 -> null. Track how each node moves from the unreversed suffix to the reversed prefix.

stepprev beforecurrsaved nextlist after rewiring
startnull1not savedreversed prefix is empty; suffix is **1 -> 2 -> 3 -> null**
1null12**1 -> null** and remaining suffix **2 -> 3 -> null**
2123**2 -> 1 -> null** and remaining suffix **3 -> null**
323null**3 -> 2 -> 1 -> null** and remaining suffix is empty

When curr becomes null, prev points to 3, which is the new head of 3 -> 2 -> 1 -> null.

Interview Tips

Name the invariant before coding: prev is the head of the reversed prefix, and curr is the head of the unreversed suffix. Then say the safety rule out loud: save curr.next before rewiring it. Many harder linked-list problems are just this move applied to a sublist, a pair, or a k-sized group.

Likely follow-ups

  • How would you reverse only positions **left** through **right**?
  • How would you reverse nodes in groups of **k**?
  • How would you detect whether reversing created an accidental cycle during debugging?
  • Can you write the same reversal recursively, and what stack-space tradeoff does it make?

Similar Problems

Key Takeaways

  • The safe reversal order is save **next**, rewire **curr.next**, then advance **prev** and **curr**.
  • The old head becomes the tail, so its next pointer must end as **null**.
  • Returning **prev** is correct because it points at the last processed node after the loop.
  • Sublist, pair, and group reversal problems build on this exact primitive.
Reusable template: In-place pointer reversal: preserve the forward link, redirect the current node backward, then slide the reversed-prefix boundary forward.