Reverse Linked List (LeetCode 206): Solution and Explanation

Easy
Linked List
Recursion

Given the head of a singly linked list, reverse the direction of every link and return the new head, which is the node that used to be last.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

An easy but wasteful approach copies every value into an array, then walks the list again writing the values back in reverse order. It uses O(n) extra memory and does not actually reverse the structure, only the data. The real task is to flip each next pointer so it points to the previous node instead of the following one.

The difficulty is that a singly linked list only lets you move forward. The moment you change curr.next to point backward, you have lost your only reference to the rest of the list. So before rewiring a node, you must save where it was going. That leads to three pointers: prev (the already reversed part, initially empty), curr (the node being rewired), and nxt (a temporary handle on the remainder of the list).

Each step does four small things in a fixed order: remember curr.next, point curr.next at prev, move prev up to curr, and move curr to the saved next node. After the step, the reversed part has grown by one node and the unreversed part has shrunk by one. When curr falls off the end, prev is sitting on the old tail, which is the head of the reversed list.

It helps to draw the list on paper with boxes and arrows and physically redraw one arrow per step. The invariant to keep in mind is that prev always heads a correctly reversed list and curr always heads the untouched remainder, with no link between the two. Once that picture is clear, the order of the four assignments follows naturally, and the same loop becomes a building block for harder problems like reversing nodes in groups of k or checking whether a list is a palindrome.

Approach: Iterative three-pointer reversal

  1. Set prev = None and curr = head.
  2. While curr is not None, save the next node with nxt = curr.next.
  3. Reverse the link by setting curr.next = prev.
  4. Advance prev to curr, so the reversed portion now starts at this node.
  5. Advance curr to nxt, the first node of the unreversed portion.
  6. When the loop ends, return prev, the new head.

Step-by-step example

head = [1, 2, 3]
  1. Start: prev = None, curr = 1. The list is 1 → 2 → 3.
  2. Step 1: nxt = 2, set 1.next = None, prev = 1, curr = 2. Reversed part: 1.
  3. Step 2: nxt = 3, set 2.next = 1, prev = 2, curr = 3. Reversed part: 2 → 1.
  4. Step 3: nxt = None, set 3.next = 2, prev = 3, curr = None. Reversed part: 3 → 2 → 1.
  5. curr is None, so the loop stops and the function returns node 3, giving [3, 2, 1].

Code: Python, Java, C++

1class Solution:
2    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
3        prev = None
4        curr = head
5        
6        while curr:
7            nxt = curr.next
8            curr.next = prev
9            prev = curr
10            curr = nxt
11            
12        return prev

Time and space complexity

Time

O(n)

The loop visits each of the n nodes once and does a constant number of pointer assignments per node.

Space

O(1)

The list is reversed in place using only three pointer variables.

Other approaches

Recursive reversal (time O(n), space O(n))

Recursively reverse the list starting at head.next, then set head.next.next = head and head.next = None, returning the new head from the deepest call. It is elegant but uses a stack frame per node and can hit recursion limits on long lists.

Stack of nodes (time O(n), space O(n))

Push every node onto a stack, then pop them to relink in reverse order. It is easy to reason about but needs O(n) extra memory, so it is rarely the preferred answer.

Common mistakes

  • Setting curr.next = prev before saving curr.next. The rest of the list becomes unreachable and the loop ends after one node.
  • Returning head or curr at the end. head is now the tail, and curr is None; the new head is prev.
  • In the recursive version, forgetting head.next = None. The original head keeps pointing at the second node, which creates a two-node cycle.
  • Advancing curr before prev. Then prev is set to the wrong node and the links get scrambled.

Frequently asked questions

How do you reverse a linked list in place?

Walk the list with three pointers. Save the next node, point the current node back at the previous one, then move both pointers forward one step. When you reach the end, the previous pointer is the new head.

Iterative or recursive: which is better for reversing a linked list?

Both are O(n) time. The iterative version uses O(1) space, while the recursive one uses O(n) stack space, so iterative is usually preferred in production and interviews.

What does the function return for an empty list?

The loop never runs because curr starts as None, so it returns prev, which is None.

How do I reverse only part of a linked list?

Walk to the node before the section, reverse just that many nodes with the same three-pointer loop, then reconnect the reversed section to the nodes before and after it.