206. Reverse Linked List
easyAsked at GleanGlean tests pointer manipulation fundamentals here — the same in-place rewiring skill that matters when you're rearranging result-list nodes in a search ranking pipeline without allocating new memory.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Glean loops.
- Glassdoor (2025-11)— Listed in Glean SWE onsite feedback as a core data-structures warm-up.
- Blind (2025-08)— Glean candidates report linked-list reversal appearing in early coding rounds to establish pointer fluency.
Problem
Given the head of a singly linked list, reverse the list, and return the reversed list's head.
Constraints
The number of nodes in the list is in the range [0, 5000].−5000 <= Node.val <= 5000.
Examples
Example 1
head = [1,2,3,4,5][5,4,3,2,1]Explanation: All next pointers are reversed so the tail becomes the head.
Example 2
head = [][]Explanation: Empty list returns null.
Approaches
1. Iterative (three-pointer)
Walk the list with three pointers — prev, curr, next. At each step, reverse the next pointer of curr to point to prev, then advance all three forward.
- Time
- O(n)
- Space
- O(1)
function reverseList(head) {
let prev = null;
let curr = head;
while (curr !== null) {
const next = curr.next; // save
curr.next = prev; // reverse
prev = curr; // advance prev
curr = next; // advance curr
}
return prev; // prev is new head
}Tradeoff: O(n) time, O(1) space. In-place with no extra allocations — the preferred answer in a systems-oriented shop like Glean.
2. Recursive
Recurse to the end of the list, then on the way back up, reverse each next pointer. The base case (null or single node) returns the new head.
- Time
- O(n)
- Space
- O(n) call stack
function reverseList(head) {
if (head === null || head.next === null) return head;
const newHead = reverseList(head.next);
head.next.next = head; // node after head now points back to head
head.next = null; // head becomes the new tail
return newHead;
}Tradeoff: Elegant but uses O(n) stack space due to recursion depth. Mention this trade-off unprompted — Glean values candidates who reason about memory.
Glean-specific tips
Glean cares about correctness under edge cases. Walk through your solution with an empty list (null head) and a single-node list before writing code — this shows systematic thinking. Prefer the iterative approach and mention that it avoids stack overflow on very long lists.
Common mistakes
- Forgetting to save curr.next before overwriting it — causes the rest of the list to be lost.
- Returning curr instead of prev at the end — curr is null after the loop; prev is the new head.
- Not handling the empty-list edge case — always check if head is null first.
- In the recursive version, not setting head.next = null — creates a cycle between the last two nodes.
Follow-up questions
An interviewer at Glean may pivot to one of these next:
- Reverse Linked List II (LC 92) — reverse only a subrange [left, right] of the list.
- Palindrome Linked List (LC 234) — check if the list reads the same forwards and backwards.
- How would you reverse a doubly-linked list?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is prev initialized to null?
The original head becomes the new tail, so its next pointer must be null. Starting prev at null achieves this automatically on the first iteration.
When would you prefer the recursive approach?
When code clarity is more important than stack-depth safety, or when working with a functional language where tail-recursion optimization applies. For production Go or Python with large lists, use the iterative version.
Practice these live with InterviewChamp.AI
Drill Reverse Linked List and other Glean interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →