39. Swap Nodes in Pairs
mediumAsked at VercelSwap every two adjacent nodes in a linked list. Vercel asks this for the pointer-juggling discipline — same skill needed to reorganize chunks in their streaming-response pipeline.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-12)— Vercel platform onsite; iterative version expected.
- Blind (2026-Q1)— Listed in Vercel runtime screen.
Problem
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed).
Constraints
The number of nodes in the list is in the range [0, 100].0 <= Node.val <= 100
Examples
Example 1
head = [1,2,3,4][2,1,4,3]Example 2
head = [][]Example 3
head = [1][1]Approaches
1. Recursive swap
If at least two nodes remain, swap the first two and recurse on the rest.
- Time
- O(n)
- Space
- O(n) stack
function swapPairs(head) {
if (!head || !head.next) return head;
const a = head, b = head.next;
a.next = swapPairs(b.next);
b.next = a;
return b;
}Tradeoff: Clean but uses O(n) stack. The iterative version is the same complexity with O(1) stack.
2. Iterative with dummy head (optimal)
Dummy prev. While next two nodes exist, swap them by rewiring prev.next, first.next, second.next.
- Time
- O(n)
- Space
- O(1)
function swapPairs(head) {
const dummy = { next: head };
let prev = dummy;
while (prev.next && prev.next.next) {
const a = prev.next;
const b = a.next;
a.next = b.next;
b.next = a;
prev.next = b;
prev = a;
}
return dummy.next;
}Tradeoff: O(1) stack. The dummy head removes the special case for the first pair. Three rewires per iteration.
Vercel-specific tips
Vercel grades the iterative version. Bonus signal: writing the three rewire operations in the correct order — a.next = b.next, then b.next = a, then prev.next = b. Mention that the recursive version is cleaner but uses stack space.
Common mistakes
- Wrong rewire order — produces cycles or losing the rest of the list.
- Forgetting the dummy — first pair becomes a special case.
- Not advancing prev to a (the new second node in the swapped pair) after each iteration.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Reverse nodes in k-group (LC 25, hard) — generalization.
- Swap every kth node value (LC 1721).
- What if the list has odd length? The last node stays in place.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why three rewires?
Two adjacent nodes a and b. After swap, the predecessor points to b, b points to a, and a points to whatever was after b. That's three pointer updates: prev->b, b->a, a->(b's old next).
Can I swap values instead of nodes?
The problem explicitly forbids it. In production code it'd be cheaper, but the interview tests pointer manipulation.
Practice these live with InterviewChamp.AI
Drill Swap Nodes in Pairs and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →