39. Swap Nodes in Pairs
mediumAsked at SnowflakeSwap every two adjacent nodes in a linked list. Snowflake asks this to test pointer-manipulation precision — the same precision required when reordering rows in their pipelined executor.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2025-Q4)— Snowflake new-grad onsite as pointer warm-up.
- LeetCode Discuss (2025-09)— Reported at Snowflake intern screens.
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. Swap values (cheating)
Walk in pairs and swap values; doesn't actually rewire nodes.
- Time
- O(n)
- Space
- O(1)
function swapPairs(head) {
let curr = head;
while (curr && curr.next) {
[curr.val, curr.next.val] = [curr.next.val, curr.val];
curr = curr.next.next;
}
return head;
}Tradeoff: Violates the constraint (must change nodes, not values). Useful only to discuss and reject.
2. Iterative pointer rewiring (optimal)
Use a dummy head. For each pair, save references to first, second, and next-pair-start; rewire dummy->second->first->next-pair.
- Time
- O(n)
- Space
- O(1)
function swapPairs(head) {
const dummy = { val: 0, next: head };
let prev = dummy;
while (prev.next && prev.next.next) {
const first = prev.next;
const second = first.next;
first.next = second.next;
second.next = first;
prev.next = second;
prev = first;
}
return dummy.next;
}Tradeoff: Pointer-only mutation. Dummy head + 'prev' generalizes to k-group reversal (LC 25).
Snowflake-specific tips
Snowflake interviewers want you to verbalize the rewiring order — first.next, second.next, prev.next — to show you control three pointers without losing references. Bonus signal: pivot to LC 25 (reverse-in-groups-of-k) and discuss its place in their executor for batch-flipping micro-partitions during ORDER BY reverse.
Common mistakes
- Forgetting dummy and not handling the head-changes case.
- Wrong order of pointer updates — overwrite first.next before saving the next pair, lose the rest of the list.
- Recursive solution overflowing the stack on 100-node lists (won't happen here, but mention it).
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- Reverse Nodes in k-Group (LC 25).
- Reverse Linked List II (LC 92).
- Recursive version (and stack-overflow analysis).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why not just swap values?
The problem prohibits it: 'you must solve the problem without modifying the values'. The point is to show pointer-rewiring skill.
Why dummy head?
Without it, the first swap needs special handling because there's no 'prev' before head. Dummy makes the first iteration look like every other.
Practice these live with InterviewChamp.AI
Drill Swap Nodes in Pairs and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →