10. Reverse Linked List
easyAsked at SquareReverse a singly linked list. Square uses this to test that you can manipulate next-pointers without losing nodes — the same discipline they expect when replaying out-of-order event logs during offline-to-online POS sync.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Square loops.
- Glassdoor (2025)— Square Reader hardware-software interface team.
- LeetCode Discuss (2026-Q1)— Cash App backend onsite warm-up.
Problem
Given the head of a singly linked list, reverse the list, and return the reversed list.
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]Example 2
head = [1,2][2,1]Example 3
head = [][]Approaches
1. Recursion
Reverse the tail, then point head.next.next = head and head.next = null.
- Time
- O(n)
- Space
- O(n)
function reverseList(head) {
if (!head || !head.next) return head;
const newHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return newHead;
}Tradeoff: Elegant but O(n) call stack — stack overflow on long lists, which Square interviewers do test.
2. Iterative three-pointer
Walk forward; at each node, redirect its next to the prev pointer.
- Time
- O(n)
- Space
- O(1)
function reverseList(head) {
let prev = null;
let cur = head;
while (cur) {
const next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}Tradeoff: Constant space. The 'save next before overwriting' move is the canonical pattern Square interviewers want to see.
Square-specific tips
Square interviewers grade this on whether you talk through pointer state with names ('prev', 'cur', 'next') and explain why you must save next BEFORE overwriting cur.next. Mention that this is the building block for reverse-in-groups (LC 25) which they sometimes follow up with. Drawing it on the whiteboard with arrows earns bonus signal.
Common mistakes
- Overwriting cur.next before saving it — you lose the rest of the list.
- Returning head at the end instead of prev — head now points to null.
- Forgetting that the final cur is null (sentinel that lets prev be the new head).
Follow-up questions
An interviewer at Square may pivot to one of these next:
- Reverse nodes in K-group (LC 25).
- Reverse a sub-range [m, n] (LC 92).
- Reverse a doubly linked list.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why save next before overwriting?
Once you do cur.next = prev, the original next pointer is gone. Saving it first lets you advance after the rewire.
Can I use a stack?
Yes — push all values, pop into a new list. But it's O(n) space without any code-clarity gain.
Practice these live with InterviewChamp.AI
Drill Reverse Linked List and other Square interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →