const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Reverse Linked List LeetCode Solution: Iterative & Recursive

Reverse Linked List (#206) is the single most frequently asked linked list problem in coding interviews. Master both the iterative three-pointer technique and the recursive approach to handle any variant an interviewer throws at you.

8 min read|

Reverse Linked List (#206): the most-asked linked list problem

Iterative and recursive — master both approaches for any interview

Reverse Linked List: The Most-Asked Linked List Problem

Reverse Linked List (#206) holds a special place in coding interviews. It is the single most frequently asked linked list problem across Amazon, Google, Meta, Microsoft, and virtually every other company that tests data structure fundamentals. If you are preparing for a coding interview, this is one you absolutely must know.

The problem statement is straightforward: given the head of a singly linked list, reverse the list and return the new head. What makes it a perennial favorite is that it tests pointer manipulation in its purest form — no hash maps, no fancy data structures, just you and the pointers.

In this walkthrough, you will learn two complete approaches: the iterative three-pointer technique (O(1) space) and the recursive approach (O(n) space). By the end, you will understand exactly when and why to use each one, and you will be ready to tackle every linked list reversal variant that builds on this foundation.

Understanding the Reverse Linked List Problem

The input is the head node of a singly linked list where each node has a value and a next pointer. Your job is to reverse the direction of every next pointer so the last node becomes the new head and the original head becomes the tail with its next set to null.

Consider the list 1 -> 2 -> 3 -> 4 -> 5. After reversal, the list becomes 5 -> 4 -> 3 -> 2 -> 1. The node that held value 5 is now the head, and the node that held value 1 now points to null.

The key constraint is that you must reverse the list in place — you cannot create new nodes. You are rearranging existing pointers. This is what makes the problem a true test of pointer manipulation skill, and it is exactly the kind of low-level thinking that interviewers want to see.

  • Input: head of a singly linked list (or null for an empty list)
  • Output: head of the reversed list
  • Constraint: reverse in place by changing next pointers, not by creating new nodes
  • Time target: O(n) where n is the number of nodes
ℹ️

Interview Frequency

Reverse Linked List (#206) is the most frequently asked linked list problem across all companies — Amazon, Google, Meta, and Microsoft all use it as a fundamentals check.

Iterative Approach: Reverse Linked List with Three Pointers

The iterative approach is the gold standard for reversing a linked list. It uses three pointer variables — prev, curr, and next — and processes one node at a time in a single pass. The result is O(n) time complexity and O(1) space complexity, which is as efficient as it gets.

The algorithm works by walking through the list and flipping each node's next pointer to point backward instead of forward. At every step you need to save the next node before you overwrite the pointer, which is why you need that third variable.

Here is the four-line core that makes up the entire algorithm. Initialize prev to null and curr to head. Then loop while curr is not null: save next = curr.next, set curr.next = prev, advance prev = curr, and advance curr = next. When the loop ends, prev points to the new head.

This approach is almost always the one you should present first in an interview. It is space-optimal, easy to reason about, and straightforward to extend to problems like Reverse Linked List II (#92) where you reverse only a subrange of the list.

  1. 1Initialize prev = null and curr = head
  2. 2While curr is not null: save next = curr.next
  3. 3Reverse the pointer: curr.next = prev
  4. 4Advance prev = curr and curr = next
  5. 5Return prev as the new head

Recursive Approach: Reverse Linked List from the End

The recursive approach solves the same problem with a different mental model. Instead of walking forward and flipping pointers, you recurse all the way to the end of the list and then reverse pointers on the way back up the call stack.

The base case is when head is null or head.next is null — in either case, you return head because a list of zero or one nodes is already reversed. For the recursive case, you call reverse(head.next) to get the new head of the reversed sublist, then set head.next.next = head and head.next = null to reverse the link between the current node and its successor.

The recursive solution is elegant and demonstrates strong understanding of recursion, which some interviewers value. However, it uses O(n) stack space because each recursive call adds a frame to the call stack. For a list with millions of nodes, this could cause a stack overflow.

When should you use the recursive approach? Present it as a follow-up after giving the iterative solution. It shows versatility and deep understanding of both iteration and recursion — exactly the kind of range that impresses interviewers.

  • Base case: if head is null or head.next is null, return head
  • Recursive case: newHead = reverse(head.next)
  • Reverse the link: head.next.next = head, then head.next = null
  • Return newHead (the tail of the original list)
  • Time: O(n), Space: O(n) due to call stack
⚠️

Stack Space Warning

The recursive approach uses O(n) stack space — for very long lists this can cause stack overflow. Always mention the iterative approach as the space-optimal alternative.

Visual Walkthrough: Reverse Linked List Step by Step

Let us trace the iterative approach on the list 1 -> 2 -> 3 -> 4 -> 5 to make the pointer manipulation concrete. Seeing each step written out removes any mystery about how the reversal works.

Start: prev = null, curr = 1. Save next = 2. Set 1.next = null (was 2). Move prev = 1, curr = 2. The list state is now: null <- 1, 2 -> 3 -> 4 -> 5.

Step 2: prev = 1, curr = 2. Save next = 3. Set 2.next = 1 (was 3). Move prev = 2, curr = 3. State: null <- 1 <- 2, 3 -> 4 -> 5.

Step 3: prev = 2, curr = 3. Save next = 4. Set 3.next = 2 (was 4). Move prev = 3, curr = 4. State: null <- 1 <- 2 <- 3, 4 -> 5.

Step 4: prev = 3, curr = 4. Save next = 5. Set 4.next = 3 (was 5). Move prev = 4, curr = 5. State: null <- 1 <- 2 <- 3 <- 4, 5.

Step 5: prev = 4, curr = 5. Save next = null. Set 5.next = 4 (was null). Move prev = 5, curr = null. Loop ends. Return prev = 5. Final list: 5 -> 4 -> 3 -> 2 -> 1 -> null.

Notice how at every step, the boundary between the reversed portion and the unreversed portion moves one node to the right. The prev pointer always marks the head of the reversed portion, and curr marks the next node to process.

Edge Cases and Reverse Linked List Variants

Before submitting your solution, make sure you handle the edge cases that interviewers love to probe. An empty list (head is null) should return null. A single-node list should return that node unchanged. Both approaches handle these naturally — the iterative loop simply does not execute, and the recursive base case returns immediately.

Once you master Reverse Linked List (#206), several important variants become approachable. Reverse Linked List II (#92) asks you to reverse only the nodes between positions left and right — you need the same three-pointer technique but with careful boundary management. Palindrome Linked List (#234) reverses the second half and compares it with the first half.

Other problems that build on this foundation include Reorder List (#143), which interleaves the first and reversed second halves, and Swap Nodes in Pairs (#24), which reverses in groups of two. The pattern of saving next, flipping the pointer, and advancing is the same in every case.

  • Empty list (null): return null — both approaches handle this
  • Single node: return head unchanged — base case covers this
  • Reverse Linked List II (#92): reverse a subrange using the same three-pointer core
  • Palindrome Linked List (#234): reverse second half, compare with first half
  • Reorder List (#143): find middle, reverse second half, merge alternately
  • Swap Nodes in Pairs (#24): reverse in groups of two
💡

Pro Tip

The iterative approach uses just 3 variables: prev, curr, and next. Save next = curr.next, reverse curr.next = prev, advance prev = curr, curr = next. These 4 lines are the entire algorithm.

What Reverse Linked List Teaches You

Reverse Linked List is more than a single problem — it is the gateway to an entire category of pointer manipulation challenges. The three-pointer technique you learn here (prev, curr, next) reappears in dozens of linked list problems. Once you internalize the pattern of saving the next reference before overwriting it, linked list problems stop being intimidating.

The problem also teaches you the importance of choosing between iteration and recursion based on constraints. The iterative approach is almost always preferred in production code for its O(1) space usage, but the recursive approach demonstrates deeper algorithmic thinking that interviewers sometimes specifically ask for.

To lock this pattern into long-term memory, practice it with spaced repetition. YeetCode flashcards break Reverse Linked List and its variants into focused review sessions so you build the muscle memory to write the three-pointer loop without hesitation. When reverse linked list leetcode appears on your interview whiteboard, you want the solution to flow automatically.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now