Reverse Linked List (LeetCode 206): Solution and Explanation
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.
Watch it run step by step
Try your own input and write the code →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
- Set
prev = Noneandcurr = head. - While
curris notNone, save the next node withnxt = curr.next. - Reverse the link by setting
curr.next = prev. - Advance
prevtocurr, so the reversed portion now starts at this node. - Advance
currtonxt, the first node of the unreversed portion. - When the loop ends, return
prev, the new head.
Step-by-step example
head = [1, 2, 3]
- Start: prev = None, curr = 1. The list is 1 → 2 → 3.
- Step 1: nxt = 2, set 1.next = None, prev = 1, curr = 2. Reversed part: 1.
- Step 2: nxt = 3, set 2.next = 1, prev = 2, curr = 3. Reversed part: 2 → 1.
- Step 3: nxt = None, set 3.next = 2, prev = 3, curr = None. Reversed part: 3 → 2 → 1.
- 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 prev1class Solution {
2 public ListNode reverseList(ListNode head) {
3 ListNode prev = null;
4 ListNode curr = head;
5
6 while (curr != null) {
7 ListNode nxt = curr.next;
8 curr.next = prev;
9 prev = curr;
10 curr = nxt;
11 }
12 return prev;
13 }
14}1class Solution {
2public:
3 ListNode* reverseList(ListNode* head) {
4 ListNode* prev = nullptr;
5 ListNode* curr = head;
6
7 while (curr) {
8 ListNode* nxt = curr->next;
9 curr->next = prev;
10 prev = curr;
11 curr = nxt;
12 }
13 return prev;
14 }
15};Time and space complexity
O(n)
The loop visits each of the n nodes once and does a constant number of pointer assignments per node.
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 = prevbefore savingcurr.next. The rest of the list becomes unreachable and the loop ends after one node. - Returning
headorcurrat the end.headis now the tail, andcurrisNone; the new head isprev. - In the recursive version, forgetting
head.next = None. The original head keeps pointing at the second node, which creates a two-node cycle. - Advancing
currbeforeprev. Thenprevis 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.