5. Reverse Linked List
easyAsked at PostmanReverse a singly linked list in place and return the new head.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given the head of a singly linked list, reverse the list and return the new head. Both iterative and recursive solutions are expected to be discussed.
Constraints
0 <= nodes <= 5000-5000 <= node.val <= 5000
Examples
Example 1
head = [1,2,3,4,5][5,4,3,2,1]Example 2
head = [][]Approaches
1. Stack
Push all values onto a stack, then rebuild a new list in popped order.
- Time
- O(n)
- Space
- O(n)
const stack = [];
while (head) { stack.push(head.val); head = head.next; }
// build new list from stack popsTradeoff:
2. Iterative pointer flip
Walk the list, flipping each node's next pointer to its predecessor. Three pointers suffice.
- Time
- O(n)
- Space
- O(1)
function reverse(head) {
let prev = null, curr = head;
while (curr) {
const next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}Tradeoff:
Postman-specific tips
Postman engineers reverse linked lists when undoing request-history breadcrumbs in the app, so they expect both iterative and recursive forms verbally.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Reverse Linked List and other Postman interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →