29. Reverse Linked List
easyAsked at SalesforceReverse a singly linked list. Salesforce uses this as a fundamental pointer-manipulation check — they expect both the iterative and recursive solutions.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Salesforce uses this as the canonical linked-list warmup before harder problems.
- Blind (2025-12)— Recurring on Salesforce backend phone screens.
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 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. Use a stack
Push all values onto a stack, then pop into a new list.
- Time
- O(n)
- Space
- O(n)
function reverseList(head) {
const stack = [];
let cur = head;
while (cur) { stack.push(cur.val); cur = cur.next; }
const dummy = { next: null };
cur = dummy;
while (stack.length) { cur.next = { val: stack.pop(), next: null }; cur = cur.next; }
return dummy.next;
}Tradeoff: O(n) space and creates new nodes. Salesforce wants in-place pointer reversal.
2. Iterative pointer reversal
Walk the list with prev/curr/next pointers. At each step, flip curr.next to point to prev.
- Time
- O(n)
- Space
- O(1)
function reverseList(head) {
let prev = null, curr = head;
while (curr) {
const next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}Tradeoff: True in-place reversal. The next-pointer save is crucial — without it, you'd lose the rest of the list when you flip curr.next.
Salesforce-specific tips
Salesforce expects this in under 2 minutes — it's the floor for backend candidates. Bonus signal: also provide the recursive version (using the elegant 'reverse rest, then attach head' pattern) and discuss when each is preferred. They use linked-list reversal in their cursor-based pagination logic.
Common mistakes
- Forgetting to save curr.next before reassigning — corrupts the list mid-walk.
- Returning head instead of prev — head is now the last node, prev is the new head.
- Initializing prev to head — flips the wrong direction.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Reverse Linked List II (LC 92) — reverse a sublist.
- Reverse Nodes in k-Group (LC 25).
- Palindrome Linked List (LC 234) — uses reversal as a subroutine.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why save curr.next before reassigning curr.next?
Once you set curr.next = prev, the original curr.next is lost. Saving it lets you advance to the next node after flipping the pointer.
Iterative vs recursive — which does Salesforce prefer?
Iterative for production (no stack-overflow risk). They expect you to know both.
Practice these live with InterviewChamp.AI
Drill Reverse Linked List and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →