Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return the new head. You must swap the nodes themselves, not just their values.
Open on LeetCode ↗Intuition
For each pair of nodes A -> B, you want B -> A, and A should point to whatever the next pair's swap produces. A dummy node before the head avoids special-casing the first pair. Walk with a prev pointer that always sits right before the next pair to swap. The pointer surgery for each pair is three reassignments: prev.next = B, A.next = B.next, B.next = A. Then advance prev to A (which is now the second node of the swapped pair) and repeat.
When a linked list problem asks you to rearrange nodes in fixed-size groups (pairs, triples, k-groups), the pattern is: use a prev pointer before the group, perform the pointer surgery, then advance prev past the rearranged group. A dummy node at the front unifies the first group with all others.
Approach
Set up a dummy node before the head
Create a dummy node with dummy.next = head. Initialize prev = dummy. The dummy absorbs the head-changing logic — after all swaps, the new head is dummy.next.
Swap each pair with three pointer reassignments
While prev.next and prev.next.next both exist, let first = prev.next and second = prev.next.next. Perform: (1) prev.next = second — the predecessor now points to the second node, (2) first.next = second.next — the first node skips over the second to point at the rest, (3) second.next = first — the second node now points back to the first. This completes the swap.
Advance `prev` past the swapped pair
After the swap, first is now the second node of the pair. Set prev = first and continue the loop. When fewer than two nodes remain, the loop ends. Return dummy.next. Time is O(n), space is O(1).
Solution
Common pitfalls
Swapping values instead of nodes
first.val, second.val = second.val, first.val
prev.next = second first.next = second.next second.next = first
The problem explicitly requires swapping nodes, not values. Value swapping is O(1) and tempting, but it violates the constraint and would fail if nodes carry additional data.
Forgetting to advance prev to first after the swap
prev = second
prev = first
After the swap, first is the second node of the pair — it is the last node before the next pair. Advancing prev to second (which is now the first node) skips over first and connects the next swap to the wrong predecessor.
Not using a dummy node, causing the head to be lost
prev = head first = head second = head.next
dummy = ListNode(0) dummy.next = head prev = dummy
Without a dummy, swapping the first pair changes the head of the list. You would need a separate variable to track the new head and special-case the first iteration. The dummy eliminates this.
Edge cases
prev.next is None, so the loop never runs. Return None.
prev.next.next is None, so no pair exists. The single node is returned unchanged.
The last node has no partner. The loop condition prev.next and prev.next.next stops before it, leaving the last node in place.