Reverse Linked List
On this page
Problem statement
Given the head of a singly linked list, reverse the list and return the new head. Change the links, not the values, and use O(1) extra space for the iterative version.
Short, common, and unforgiving: three pointers, four lines, and one wrong order loses the rest of the list. It's also a building block for harder problems (reverse in groups, palindrome lists, reorder list). Original problem: LeetCode 206.
Example

Input: 1 -> 2 -> 3 -> 4 -> null
Output: 4 -> 3 -> 2 -> 1 -> null
Input: null
Output: null
Constraints (assumed)
- 0 ≤ number of nodes ≤ 5,000.
What this question tests
- Careful pointer manipulation in the right order.
- Knowing both the iterative (O(1) space) and recursive versions and their costs.
- Handling empty and one-node lists without special cases.
Questions to ask first
- Can I modify the list in place? (Here: yes; reverse the links.)
- Singly or doubly linked? (Here: singly.)
- Iterative or recursive? Either is accepted; mention the stack cost of recursion.
- Reverse the whole list or a part? (Here: the whole list; partial reversal is a follow-up.)
Try it yourself first
You have the full problem. Sketch your answer, then sign up free to read the complete walkthrough.
- The complete design, flow by flow, with pictures
- Deep dives into the hard parts
- What mid, senior and staff candidates are expected to say
- Full company question lists
Free forever. No card needed.