206. Reverse Linked List
Last updated
Was this helpful?
Last updated
Was this helpful?
🟩 Easy
Given the head
of a singly linked list, reverse the list, and return the reversed list.
Input: head = [1,2,3,4,5] Output: [5,4,3,2,1]
Input: head = [1,2] Output: [2,1]
Input: head = [] Output: []
The number of nodes in the list is the range [0, 5000]
.
-5000 <= Node.val <= 5000
Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?
My Solution (Iterative)
Optimal Solution (Recursive)
This classic problem can be solved in two main ways:
Iterative Approach (Your Solution):
Uses three pointers for explicit control
Reverses links one by one
Very space efficient
Great for interviews
Recursive Approach (Optimal):
Elegant divide-and-conquer
Reverses from back to front
More mathematical
Cleaner code
Time: O(n)
Single pass through list
Constant work per node
No repeated work
Space: O(1)
Only three pointers
Constant extra space
True in-place reversal
Time: O(n)
Visits each node once
Constant work per node
Same as iterative
Space: O(n)
Recursive call stack
One frame per node
Not truly in-place
Iterative Approach:
Maintains clear invariants
Never loses track of nodes
Very mechanical process
Easy to visualize
Recursive Approach:
Assumes subproblem solved
Works backwards elegantly
More mathematical thinking
Cleaner implementation
Choose Iterative When:
Memory is constrained
Large input expected
Maximum performance needed
Interview setting
Choose Recursive When:
Code clarity priority
Small to medium input
Teaching/learning
Quick implementation needed
Similar Problems:
Reverse Linked List II
Palindrome Linked List
Swap Nodes in Pairs
Key Techniques:
Multiple pointer manipulation
Recursion on linked structure
Link reversal
Stack usage (implicit/explicit)
Discuss Both Approaches:
Mention space trade-offs
Explain time complexity
Discuss pros and cons
Show knowledge depth
Common Pitfalls:
Not saving next pointer
Stack overflow in recursion
Infinite loops
Lost references
Testing Strategy:
Empty list
Single node
Two nodes
General case
Check all links
Leetcode: