Reverse Linked List — Coding Interview Walkthrough
You’re in a coding interview. The interviewer says: “Reverse a singly linked list in place.” You know the three-pointer technique. But under pressure, candidates who studied this solution lose track of which pointer to advance, return current instead of prev, or initialize prev to head and create an unintended cycle. One wrong step and you’ve either lost the rest of the list or created a loop.
This walkthrough covers the three-pointer iterative technique, the recursive approach with its stack space tradeoff, every pointer mistake to avoid, and the follow-up questions interviewers use to test depth.
The Problem
Given the head of a singly linked list, reverse the list and return the new head. The reversal must be done in place by relinking the existing nodes. (LeetCode #206)
Example
Input
head = [1,2,3,4,5]
Output
[5,4,3,2,1]
The original list 1 → 2 → 3 → 4 → 5 → None becomes 5 → 4 → 3 → 2 → 1 → None. Each node’s next pointer is redirected to point backward. Node 1 becomes the tail, node 5 becomes the new head. No new nodes are created.
Already comfortable with the solution? Practice it in a mock interview →
What Interviewers Are Actually Testing
The problem is short enough that implementation is everything. Interviewers watch exactly how you manage the three-pointer state.
| Skill | What Interviewers Observe |
|---|---|
| Three-pointer management | Do you correctly save next_node before redirecting current.next? |
| Loop termination | Do you use while current (not while current.next) and understand why? |
| Pointer sequencing | Do you apply save, redirect, shift prev, shift current in order? |
| Recursive thinking | Can you model the reversal as a self-similar subproblem on the tail? |
| Space tradeoff awareness | Do you note recursive uses O(n) stack space vs. iterative’s O(1)? |
| Follow-up readiness | Can you reverse a sublist (#92) or reverse in k-groups (#25)? |
Step 1: Clarifying Questions
-
Should I reverse in place by relinking nodes? Yes. No new nodes, no array collection.
-
What about empty or single-node lists? Return
Nonefor empty, return the node for single. Both handled naturally by the algorithm. -
Singly or doubly linked? Singly. No
prevpointers available. -
Can the list contain cycles? No. Standard acyclic list.
Step 2: A First (Non-Optimal) Idea
Collect values into an array, rebuild in reverse. O(n) time and space, but creates new nodes and misses the point.
def reverseList(head):
values = []
current = head
while current:
values.append(current.val)
current = current.next
dummy = ListNode(0)
current = dummy
for val in reversed(values):
current.next = ListNode(val) # Creates new nodes
current = current.next
return dummy.next
| Approach | Time | Space |
|---|---|---|
| Array-based (naive) | O(n) | O(n) |
| Iterative three-pointer | O(n) | O(1) |
| Recursive | O(n) | O(n) |
Step 3: The Key Insight
To reverse a linked list, redirect one pointer at a time. To do it safely, track three things: where you came from (
prev), where you are (current), and where you’re going (next_node).
Four steps per iteration:
- Save
next_node = current.nextbefore breaking the forward link. - Redirect
current.next = prevto flip the pointer backward. - Advance
prev = current. - Advance
current = next_node.
When current reaches None, prev is the new head.
The loop condition is while current, not while current.next. Using while current.next skips the last node.
Step 4: Optimal Strategy (Iterative)
- Initialize
prev = None,current = head. - While
current is not None:- Save
next_node = current.next. - Set
current.next = prev. - Set
prev = current. - Set
current = next_node.
- Save
- Return
prev.
Python Implementation
Iterative — Three Pointers (Recommended)
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverseList(head: ListNode) -> ListNode:
prev = None # Will become the new tail anchor
current = head # Start at the head
while current is not None:
next_node = current.next # Step 1: Save next before breaking link
current.next = prev # Step 2: Flip pointer backward
prev = current # Step 3: Advance prev
current = next_node # Step 4: Advance current
return prev # prev is the new head (original tail)
Recursive — Elegant but O(n) Stack Space
def reverseList(head: ListNode) -> ListNode:
# Base case: empty list or single node
if head is None or head.next is None:
return head
# Recurse to the end — new_head is the original last node
new_head = reverseList(head.next)
# On the way back: make head.next point back to head
head.next.next = head
head.next = None # Break the original forward link
return new_head # Propagate the new head all the way up
Key implementation notes:
next_nodemust be saved first. Oncecurrent.next = prevexecutes, the forward reference is gone. Without saving it, the rest of the list becomes unreachable.prevstarts asNone. The original head becomes the new tail, pointing toNone.- Return
prev, notcurrent. When the loop exits,currentisNone.previs the last processed node: the new head. - In the recursive version,
head.next.next = headthenhead.next = Noneis the crux. Withouthead.next = None, a cycle is created. - Recursive uses O(n) stack space, one frame per node. Approaches Python’s default recursion limit for lists over 1,000 nodes.
Time & Space Complexity
| Approach | Time | Space |
|---|---|---|
| Iterative (three pointers) | O(n) | O(1) |
| Recursive | O(n) | O(n) |
Time: Both visit every node once. O(n).
Space (iterative): Three pointer variables. True O(1).
Space (recursive): One stack frame per node. O(n), with stack overflow risk for long lists.
Common Interview Mistakes
-
Forgetting to save
next_nodebefore redirecting. The most catastrophic bug. The rest of the list becomes unreachable. -
Returning
currentinstead ofprev.currentisNoneat loop exit. The reversed list is silently discarded. -
Using
while current.nextinstead ofwhile current. Skips the last node entirely. -
In the recursive version, forgetting
head.next = None. Creates a cycle between the last two nodes. -
Initializing
prev = headinstead ofprev = None. The first node’snextpoints to itself, creating a length-1 cycle. -
Not narrating the pointer state. The four steps are mechanical. Coding silently gives the interviewer no way to verify understanding.
What a Strong Candidate Sounds Like
“I’ll use three pointers:
prevstarts atNone,currentathead. Each iteration: savenext_nodefirst because I’m about to break the forward link. Then flipcurrent.next = prev. Then advance both. WhencurrenthitsNone,previs the new head.O(n) time, O(1) space. I can also do this recursively: recurse to the end, then on the way back set
head.next.next = headandhead.next = None. Elegant but O(n) stack space.”
Example Interview Dialogue
Interviewer: Reverse a singly linked list in place.
Candidate: To confirm: reroute existing next pointers, not create new nodes?
Interviewer: Correct.
Candidate: Three pointers. prev = None, current = head. Each step: save next_node, flip current.next = prev, advance both. When current is None, return prev as the new head.
Interviewer: Why save next_node first?
Candidate: Because current.next = prev severs the link to the rest of the list. Without saving next_node, every node after current becomes unreachable. There’s no way to continue the traversal.
Interviewer: How would you reverse only positions left to right?
Candidate: That’s LeetCode #92. Traverse to the node before position left, apply the same three-pointer reversal for right - left steps, then reattach: the node before left points to the new sublist head, and the original sublist head points to the node after right. Same O(n) time, O(1) space.
Related Problems
Reverse Linked List is the foundation for an entire family of linked list manipulation problems:
- Reverse Linked List II (LeetCode #92). Reverse only positions
lefttoright. Same technique with boundary management. - Reverse Nodes in k-Group (LeetCode #25). Reverse every k nodes. Repeatedly applies the reversal.
- Palindrome Linked List (LeetCode #234). Find midpoint, reverse second half, compare. Uses reversal as a subroutine.
- Swap Nodes in Pairs (LeetCode #24). Swap every two adjacent nodes. Constrained reversal.
Practice This in a Mock Interview
The three-pointer sequence feels logical when reading: save, flip, advance, advance. But under pressure, candidates lose track of which pointer is which, return the wrong variable, or initialize prev incorrectly. The only way to make the four-step sequence truly automatic is to trace through it by hand, narrating each pointer’s value at every step.
Start a mock interview for Reverse Linked List on Intervu.
Related Problems to Practice
- Merge Two Sorted Lists — Core pointer manipulation for merging.
- Linked List Cycle — Two-pointer technique on linked lists.
- Middle of the Linked List — Slow/fast pointer pattern.
Further reading:
- How to Prepare for a Coding Interview in 2026, the complete roadmap and study plans
- Why LeetCode alone isn’t enough, and what to practice instead
- Practice any LeetCode problem as a live mock interview
- The Grind 75 Pathway, a structured study plan with AI practice links
- Browse all walkthroughs on GitHub, condensed solutions with code