📖 7 min read
Last updated on

Reverse Linked List — Coding Interview Walkthrough


Hero Image for Reverse Linked List

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.

Before: linked list 1→2→3→4→5→∅. After: linked list 5→4→3→2→1→∅. Each node's next pointer is redirected backward.

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.

SkillWhat Interviewers Observe
Three-pointer managementDo you correctly save next_node before redirecting current.next?
Loop terminationDo you use while current (not while current.next) and understand why?
Pointer sequencingDo you apply save, redirect, shift prev, shift current in order?
Recursive thinkingCan you model the reversal as a self-similar subproblem on the tail?
Space tradeoff awarenessDo you note recursive uses O(n) stack space vs. iterative’s O(1)?
Follow-up readinessCan you reverse a sublist (#92) or reverse in k-groups (#25)?

Step 1: Clarifying Questions

  1. Should I reverse in place by relinking nodes? Yes. No new nodes, no array collection.

  2. What about empty or single-node lists? Return None for empty, return the node for single. Both handled naturally by the algorithm.

  3. Singly or doubly linked? Singly. No prev pointers available.

  4. 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
ApproachTimeSpace
Array-based (naive)O(n)O(n)
Iterative three-pointerO(n)O(1)
RecursiveO(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:

  1. Save next_node = current.next before breaking the forward link.
  2. Redirect current.next = prev to flip the pointer backward.
  3. Advance prev = current.
  4. 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)

  1. Initialize prev = None, current = head.
  2. While current is not None:
    • Save next_node = current.next.
    • Set current.next = prev.
    • Set prev = current.
    • Set current = next_node.
  3. Return prev.

Python Implementation

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_node must be saved first. Once current.next = prev executes, the forward reference is gone. Without saving it, the rest of the list becomes unreachable.
  • prev starts as None. The original head becomes the new tail, pointing to None.
  • Return prev, not current. When the loop exits, current is None. prev is the last processed node: the new head.
  • In the recursive version, head.next.next = head then head.next = None is the crux. Without head.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

ApproachTimeSpace
Iterative (three pointers)O(n)O(1)
RecursiveO(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

  1. Forgetting to save next_node before redirecting. The most catastrophic bug. The rest of the list becomes unreachable.

  2. Returning current instead of prev. current is None at loop exit. The reversed list is silently discarded.

  3. Using while current.next instead of while current. Skips the last node entirely.

  4. In the recursive version, forgetting head.next = None. Creates a cycle between the last two nodes.

  5. Initializing prev = head instead of prev = None. The first node’s next points to itself, creating a length-1 cycle.

  6. 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: prev starts at None, current at head. Each iteration: save next_node first because I’m about to break the forward link. Then flip current.next = prev. Then advance both. When current hits None, prev is 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 = head and head.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.


Reverse Linked List is the foundation for an entire family of linked list manipulation problems:


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.


Further reading:

Practice Like It's the Real Interview

Get instant feedback on your approach, communication, and code — powered by AI.

Start a Mock Interview →