39. Swap Nodes in Pairs
mediumAsked at SalesforceSwap every two adjacent nodes in a linked list. Salesforce uses this to test pointer dexterity — they want clean iterative code without value swaps.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Asked occasionally on Salesforce backend phone screens.
- LeetCode Discuss (2025)— Used to test 'no value swap' rule on linked lists.
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
Walk pairs of nodes; swap their values.
- Time
- O(n)
- Space
- O(1)
function swapPairs(head) {
let cur = head;
while (cur && cur.next) {
[cur.val, cur.next.val] = [cur.next.val, cur.val];
cur = cur.next.next;
}
return head;
}Tradeoff: Violates the problem's explicit constraint (no value mutation). Salesforce will fail this.
2. Iterative pointer reassignment with dummy head
Use prev/first/second pointers. Reroute: prev -> second -> first -> first.next.
- Time
- O(n)
- Space
- O(1)
function swapPairs(head) {
const dummy = { next: head };
let prev = dummy;
while (prev.next && prev.next.next) {
const first = prev.next, second = first.next;
first.next = second.next;
second.next = first;
prev.next = second;
prev = first;
}
return dummy.next;
}Tradeoff: Pointer-only manipulation. The dummy head eliminates the special case for the original head.
Salesforce-specific tips
Salesforce tests this to ensure you can swap NODES (not values). Bonus signal: explain the order of the three reassignments — first.next must come before second.next so you don't lose the rest of the list.
Common mistakes
- Doing the reassignments in the wrong order — losing the rest of the list.
- Forgetting the dummy head — special-casing the original head adds bugs.
- Off-by-one on the loop condition (prev.next.next must be checked).
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Reverse Nodes in k-Group (LC 25) — generalizes to swaps of k.
- Reverse Linked List II (LC 92).
- Rotate List (LC 61).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why the order: first.next = second.next, then second.next = first, then prev.next = second?
Reverse order: save the rest of the list first (first.next = second.next), then flip the pair (second.next = first), then attach to prev (prev.next = second). Any other order loses pointers.
Is the recursive version simpler?
Slightly. Recurse on rest, swap the front pair, attach. But Salesforce prefers iterative for production-safety.
Practice these live with InterviewChamp.AI
Drill Swap Nodes in Pairs 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 →