Swap Nodes in Pairs
Given the head of a linked list, swap every two adjacent nodes and return the new head. Swap the actual nodes by rewiring pointers — do not merely swap their values. If the list has an odd number of nodes, the final lone node stays in place. Empty and single-node lists are returned unchanged.
- The number of nodes is in the range [0, 100]
- 0 <= Node.val <= 100
- Solve by rewiring pointers, not by modifying node values.
head = [1,2,3,4][2,1,4,3]head = [1,2,3][2,1,3]head = [1][1]This is Reverse Nodes in k-Group frozen at k = 2: reverse the list two nodes at a time, so (1,2,3,4) becomes (2,1,4,3). Because the block is exactly two, we can write the swap directly with named pointers instead of a reversal loop — which makes it the clearest place to see the pointer surgery up close.
Swapping an adjacent pair first and second is not one rewire but three, because a node in a list is held by whoever points at it. Whatever came before the pair currently points at first; it must now point at second. second must point at first. And first must point at whatever came after the pair. Miss any one of the three and the list either loses its front, breaks in the middle, or drops its tail.
The "whatever came before the pair" is the awkward part for the very first pair — the head has nothing before it. So we prepend a dummy node pointing at the head; now every pair, including the first, has a real predecessor to rewire. We keep a prev pointer on that predecessor and step it forward two nodes after each swap.
dummy = Node(0, head)
prev = dummy
while prev.next and prev.next.next: # need two nodes ahead to swap
first = prev.next
second = first.next
# the three rewires
first.next = second.next # 1. first now points past the pair
second.next = first # 2. second points back at first (the swap)
prev.next = second # 3. predecessor adopts second as the new front
prev = first # first is now the pair's tail; advance past it
return dummy.nextfirst.next = second.next first — cache where the pair should continue to — because the next line (second.next = first) overwrites second.next and would otherwise lose the rest of the list. Only after the pair is internally swapped do we let prev adopt second. Then advance prev to first, not to second: after the swap first is the back of the pair, so it is the correct predecessor for the next pair. Send prev to the wrong node and you either re-swap the same pair forever or skip one.Each node is touched once; we hold a constant handful of pointers: O(N) time, O(1) space. The lesson is the three-rewire discipline itself — a node is only ever held by its predecessor, so relocating a node means updating whoever points at it, what it points at, and where its old target goes, in an order that caches the forward link before overwriting it. That same choreography, generalized to k nodes, is Reverse Nodes in k-Group; scaled up to whole sublists, it underlies Reverse Linked List II and Reorder List.
Structural Node Swapping Intelligence
Phase Details
Phase 1: Foundation
We use a Dummy Node to anchor the list. This allows us to handle the head swap naturally. Prev keeps track of our position.
Think of it as a leapfrog movement. You bridge the gap between two nodes, swap them, then hop over them to the next pair.
Time Complexity: O(N) | Space Complexity: O(1). Pure link manipulation without swapping values.