11. Reverse Linked List
easyAsked at KlarnaReverse a singly linked list in place.
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 node.
Constraints
0 <= list length <= 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 copy
Push values onto a stack, then rebuild the list.
- Time
- O(n)
- Space
- O(n)
function reverseList(head) {
const vals = [];
for (let c = head; c; c = c.next) vals.push(c.val);
let c = head;
while (c) { c.val = vals.pop(); c = c.next; }
return head;
}Tradeoff:
2. Iterative pointer flip
Walk the list once, flipping each next pointer using a prev cursor. Constant memory and works on the original node objects.
- Time
- O(n)
- Space
- O(1)
function reverseList(head) {
let prev = null, cur = head;
while (cur) {
const next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}Tradeoff:
Klarna-specific tips
Klarna engineers care about whether you can mutate the installment-ledger node objects in place without allocating; copy-rebuild is a red flag in their pointer-discipline rubric.
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 Klarna interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →