206. Reverse Linked List
Asked at Google, Meta, Amazon, Microsoft and 4 more
Difficulty
Easy
Time
O(n)
Space
O(1)
Pattern
Linked List
Companies
8
Problem
Given the head of a singly linked list, turn the list around so the last node becomes the first, and return the new head.
You only have next pointers to work with, so the reversal is about repointing links — not about moving values.
Every arrow flips
Constraints
- ●
The number of nodes is in the range [0, 5000] - ●
-5000 <= Node.val <= 5000
Examples
head = [1,2,3,4,5][5,4,3,2,1]head = [1,2][2,1]head = [][]Asked At
8 companies| Company | Difficulty | |
|---|---|---|
| Easy | View all Google questions → | |
| Meta | Easy | View all Meta questions → |
| Amazon | Easy | View all Amazon questions → |
| Microsoft | Easy | View all Microsoft questions → |
| Apple | Easy | View all Apple questions → |
| Uber | Easy | View all Uber questions → |
| Walmart | Easy | View all Walmart questions → |
| TikTok | Easy | View all TikTok questions → |
How to Think About It
Use three pointers: prev (starts null), current (starts at head), next (temporary). At each step, reverse the current node's pointer to point backward.
The four-step dance at each node: (1) save next = current.next, (2) current.next = prev, (3) prev = current, (4) current = next.
When current becomes null, prev is the new head. The old head is now the tail (its next is null).
Recursive approach: reverseList(head.next) does the heavy lifting. Then head.next.next = head, head.next = null. Elegant but O(n) stack space.
Optimal Approach
Iterative:
Step 1: Initialize prev = null, curr = head.
Step 2: While curr is not null:
- Save next = curr.next
- curr.next = prev (reverse the pointer)
- prev = curr (move prev forward)
- curr = next (move curr forward)
Step 3: Return prev (the new head).
The key insight: you're reversing one pointer at a time. By the time curr reaches null, every pointer has been flipped.
Time: O(n). Space: O(1).
Walk with three pointers
What Trips People Up in Real Interviews
Not handling the edge case of an empty list or a single node. Return null or the node itself.
Losing track of the next node before reassigning pointers. Save next = current.next before setting current.next = prev.
Confusing iterative and recursive approaches. The iterative approach uses three pointers (prev, current, next). The recursive approach is elegant but uses O(n) stack space.
Forgetting to update the head. After the loop, the new head is prev (the last node you processed).
Trying to reverse by creating new nodes instead of re-pointing existing ones. The problem expects in-place reversal by changing pointers, not building a new list with copied values.
Solution Code
O(n) timeclass ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverseList(head):
prev, curr = None, head
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
return prevFrequently Asked Questions
What is the Reverse Linked List problem?
Given the head of a singly linked list, reverse it and return the reversed list. This is one of the most fundamental linked list problems and a building block for many harder questions.
How do you solve Reverse Linked List?
The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Reverse Linked List?
Reverse Linked List is asked at Google, Meta, Amazon, Microsoft, Apple, Uber, Walmart, TikTok. It is a easy difficulty problem.
What are common mistakes on Reverse Linked List?
- Not handling the edge case of an empty list or a single node. Return `null` or the node itself.
- Losing track of the next node before reassigning pointers. Save next = current.next before setting current.next = prev.
- Confusing iterative and recursive approaches. The iterative approach uses three pointers (prev, current, next). The recursive approach is elegant but uses `O(n)` stack space.
- Forgetting to update the head. After the loop, the new head is prev (the last node you processed).
- Trying to reverse by creating new nodes instead of re-pointing existing ones. The problem expects in-place reversal by changing pointers, not building a new list with copied values.