206. Reverse Linked List - Detailed Explanation
Problem statement
Given the head of a singly linked list, reverse the list and return the reversed list.
In other words, you need to change the next pointers of all the nodes so that the list is reversed.
This question defines the In-place Reversal of a Linked List pattern, the technique that redirects existing next pointers one at a time instead of building a second list. Interviewers use it as the base case for every harder list question, and it is one of the recurring coding interview patterns worth drilling early. It also sits on the Blind 75 list.
Example inputs and outputs
-
Example 1:
- Input:
head = [1, 2, 3, 4, 5] - Output:
[5, 4, 3, 2, 1] - Explanation:
The linked list is reversed so that the new head is the node with value 5, followed by nodes with values 4, 3, 2, and 1.
- Input:
-
Example 2:
- Input:
head = [1, 2] - Output:
[2, 1] - Explanation:
The two nodes swap positions.
- Input:
-
Example 3:
- Input:
head = [] - Output:
[] - Explanation:
An empty list remains empty after reversal.
- Input:
Hints
-
Iterative approach using pointers:
Think about maintaining two pointers, one for the previous node and one for the current node. As you traverse the list, you point the next pointer of the current node back at the previous node. -
Recursive approach:
The recursive solution reverses the rest of the list first, then repairs the pointer of the current node.
Approaches
1. Iterative approach
Idea:
- Initialize two pointers:
prev(set toNone) andcurr(set to the head). - Traverse the list and for each node:
- Save the next node.
- Point the next pointer of the current node back at the previous node.
- Move the
prevandcurrpointers one step forward.
- Return
prevas the new head whencurrbecomesNone.
Steps:
- Set
prev = Noneandcurr = head. - While
curris notNone:- Temporarily store
curr.next. - Set
curr.nexttoprev, which reverses the link. - Move
prevtocurrandcurrto the stored next node.
- Temporarily store
- Return
prevas the head of the reversed list.
2. Recursive approach
Idea:
- The base case is an empty list or a list with one node.
- Recursively reverse the sublist starting from the second node.
- Adjust the pointers so that the next node points back to the current node. Then set the next pointer of the current node to
None.
Steps:
- If
headisNoneorhead.nextisNone, returnhead. - Otherwise, recursively reverse the list from
head.nextonward. - Set
head.next.nexttoheadto reverse the link. - Set
head.nexttoNoneto break the original link. - Return the new head from the recursive call.
Code implementations
Python code
Java code
Complexity analysis
-
Time Complexity:
- Both the iterative and recursive approaches traverse each node exactly once, so the time complexity is O(n), where n is the number of nodes in the list.
-
Space Complexity:
- Iterative approach: Uses constant extra space, O(1).
- Recursive approach: Uses O(n) space in the recursion stack.
Step by step walkthrough (iterative approach)
-
Initialization:
Setprev = Noneandcurr = head, the first node of the list. -
Iteration:
- While
curris not null:- Save
curr.nextin a temporary variable (next_temp). - Change
curr.nextto point toprev, which reverses that link. - Move
prevtocurrandcurrtonext_temp.
- Save
- While
-
Termination:
Whencurrbecomes null,prevpoints to the new head of the reversed list. -
Return:
Returnprevas the head of the reversed linked list.
Common mistakes
-
Losing the next node:
Changingcurr.nextbefore you save it discards access to the rest of the list. -
Incorrect pointer updates:
Updating the pointers in the wrong order leaves a broken list or an infinite loop. -
Forgetting to handle empty lists:
Confirm that the algorithm returnsNone, or an empty list, when the input is empty.
Edge cases and alternative variations
-
Edge cases:
- Empty list:
If the input list is empty, returnNone. - Single node:
A list with one node stays unchanged after reversal.
- Empty list:
-
Alternative variations:
- Partial reversal:
Questions that ask you to reverse only a portion of the list. - Reversing in groups:
Reversing nodes in groups, for example every k nodes.
- Partial reversal:
Related problems for further practice
- Reverse Nodes in k-Group:
Reverse nodes of a linked list in groups of size k. - Swap Nodes in Pairs:
Swap every two adjacent nodes in a linked list. - Linked List Cycle Detection:
Determine whether a linked list has a cycle.
Each of those reuses the same three pointer move you just wrote. For a short map of which pattern covers which question, read the summary of the most common LeetCode patterns. The pattern itself, with the k-group and sublist variants worked out in full, is taught in Grokking the Coding Interview.

GET YOUR FREE
Coding Questions Catalog

$99

$197

$72