Last Updated: March 29, 2026
We need to take every consecutive pair of nodes in a linked list and swap their positions. So node 1 and node 2 swap, node 3 and node 4 swap, and so on. If the list has an odd number of nodes, the last node stays where it is.
The important constraint here is that we can't just swap values. We have to actually rewire the node pointers. This makes the problem more interesting because swapping two nodes in a linked list requires updating not just the two nodes themselves, but also the node that points to them. If you forget about the previous node's pointer, you'll break the chain.
The key observation is this: for each pair, you need to track three things: the node before the pair (so you can rewire its next), the first node in the pair, and the second node in the pair. A dummy node before the head simplifies the first swap since the head itself has no predecessor.
0 <= n <= 100 → With at most 100 nodes, any approach from O(n) to O(n^2) is fine. This is a small input size, so the focus is on correctness and clean pointer manipulation, not performance.0 <= Node.val <= 100 → Values are non-negative integers. Not relevant to the algorithm since we're manipulating pointers, not values.Cannot modify node values → This rules out the easy shortcut of just swapping .val between adjacent nodes. We must physically rearrange the nodes.The recursive approach breaks the problem into a beautifully simple subproblem. Here's the idea: if we have at least two nodes, we swap the first two, then recursively handle the rest of the list. The base case is when there are fewer than two nodes remaining, in which case nothing needs to happen.
The "aha moment" is realizing that after swapping nodes 1 and 2, node 1 (now in the second position) should point to the result of recursively swapping the rest of the list starting from node 3. This naturally chains all the swaps together.
first = head and second = head.next.second.next (the third node onward).first.next to the result of the recursive call.second.next to first.second as the new head of this swapped pair.This approach works correctly but uses O(n) stack space. Can we swap pairs iteratively using a pointer that walks through the list, eliminating the recursion stack entirely?
The iterative approach processes pairs from left to right using a pointer that tracks the node just before each pair. Here's the trick: we create a dummy node that points to the head. This dummy acts as the "previous" node for the first pair, which elegantly handles the edge case of updating the head pointer.
For each pair, we need to do three pointer rewirings:
next should point to the second node (which becomes the new first in the pair).next should point to whatever comes after the pair.next should point to the first node.After the swap, the first node (which is now in the second position) becomes the "previous" node for the next pair. This is a subtle but important detail that many people get wrong.
The dummy node pattern is a classic linked list technique. By placing a sentinel node before the head, every real node in the list has a predecessor. This means the first pair is handled exactly the same as every other pair, with no special-case code.
The three pointer rewirings happen in a specific order that matters. We set prev.next = second first (connecting the previous part to the new front of the pair), then first.next = second.next (connecting the back of the pair to the rest of the list), and finally second.next = first (completing the swap within the pair). If you change this order, you risk losing references to nodes you still need.
dummy.next = head.prev = dummy.prev.next and prev.next.next are not null (there's a complete pair):first = prev.next and second = prev.next.next.prev.next = second, first.next = second.next, second.next = first.prev = first (first is now in the second position of the swapped pair).dummy.next.dummy, prev, first, second). No recursion stack, no extra data structures.