Swap Nodes in Pairs
Swap Nodes in Pairs is LeetCode 24 (Medium). Given the head of a singly linked list, swap every two adjacent nodes and return the head of the changed list.
- Change the links between nodes; changing the values stored in them is not allowed.
- When the length is odd, the last node keeps its place.
The list has at most 100 nodes, so the challenge is getting the pointer changes right, not speed.
- The number of nodes in the list is in the range [0, 100].
- 0 <= Node.val <= 100
Intuition
To swap adjacent nodes in a linked list, look at one pair a → b. Swapping it changes three links, not two: the link into the pair, the link between a and b, and the link out of the pair. The link into the pair belongs to the node before a, so keep a pointer prev to that node.
The first pair has no node before it. A dummy node in front of the head gives it one, so every pair is handled the same way, and the new head is simply dummy.next.
After the swap, a is the second node of its pair, which makes it prev for the next pair.
Any linked-list task that rewires nodes near the head, such as removing, reversing or swapping, gets simpler with a dummy node, because the head stops being a special case.
Approach
Before reading on, draw prev → 1 → 2 → 3 and list which next pointers must change to get prev → 2 → 1 → 3. In what order can you change them without losing node 3? Aim for O(1) extra space.
Two ways to solve it
Walk the list with a pointer to the node before each pair and relink the pair in place.
- Constant space: four pointers, no recursion.
- Uniform: the dummy node makes the first pair ordinary.
The usual interview answer.
Swap the first two nodes and attach the swapped rest of the list behind them.
- Short: five lines.
- Cost: one stack frame per pair.
Pick it when brevity matters more than memory.
Both run in O(n); the loop wins because it needs no call stack. The steps, code and live demo follow the loop, and the recursive code comes after the demo.
Put a dummy node in front
Create dummy with dummy.next = head and set prev = dummy. Now the first pair has a node before it like every later pair, and the answer is dummy.next at the end.
Check that a full pair is left
Loop while prev.next and prev.next.next both exist. If only one node or none remains, there is nothing to swap, which is how an odd last node stays in place.
Change the three links
Call the pair a = prev.next and b = a.next. Set prev.next = b, then a.next = b.next, then b.next = a. The second change saves the rest of the list in a.next before the third one overwrites b.next, its only other pointer.
Step past the pair
Set prev = a. After the swap a is second in its pair, so it is the node right before the next pair.
Code in Python, C++ and Java
prev starts there. Now the first pair also has a node in front of it, so it is swapped exactly like every later pair.prev.next and prev.next.next exist, so a full pair is waiting: a = 1 and b = 2. Three links have to change: the one into the pair (from dummy), the one between a and b, and the one out of the pair (to node 3).a still holds it, so nothing is lost.b.next is the only other pointer to the rest of the list.prev moves to it.prev.next and prev.next.next exist, so a full pair is waiting: a = 3 and b = 4. Three links have to change: the one into the pair (from node 1), the one between a and b, and the one out of the pair (to null).a still holds it, so nothing is lost.b.next is the only other pointer to the rest of the list.prev moves to it.dummy.next, which is node 2, not the old head 1.prev starts there. Now the first pair also has a node in front of it, so it is swapped exactly like every later pair.prev.next and prev.next.next exist, so a full pair is waiting: a = 1 and b = 2. Three links have to change: the one into the pair (from dummy), the one between a and b, and the one out of the pair (to node 3).a still holds it, so nothing is lost.b.next is the only other pointer to the rest of the list.prev moves to it.dummy.next, which is node 2, not the old head 1.Recursive swap
If fewer than two nodes remain, return the list as it is. Otherwise second becomes the new front, the old head points to the swapped rest of the list, and second points to the old head.
Common pitfalls
Returning the old head
return head
return dummy.next
head still points at the old first node, which is now second. For [1, 2, 3, 4] returning it gives [1, 4, 3], and node 2 is lost.
Moving prev to the wrong node
prev = b
prev = a
After the swap the order is b → a, so b is first, not last. For [1, 2, 3, 4] the next round swaps nodes 1 and 3 and the result is [2, 3, 4, 1] instead of [2, 1, 4, 3].
Edge cases
The loop condition fails at once and dummy.next is returned, which is head itself (None for an empty list).
Complexity
dummy, prev, a and b are extra.Swap Nodes in Pairs FAQ
Why not just swap the values?
The problem forbids it, and for a reason: in real code a node can carry a large payload or be referenced from elsewhere, so moving data is not the same as moving nodes. The exercise is about relinking.
How does this relate to Reverse Nodes in k-Group?
Swapping pairs is the k = 2 case of Reverse Nodes in k-Group. For larger k you first check that k nodes remain, reverse them in a loop and reconnect both ends; with k = 2 the reversal is the three link changes here.