Swap Nodes in Pairs: The Pointer Rewiring Classic
Swap Nodes in Pairs (#24) is one of the most instructive linked list problems on LeetCode. It asks you to take a linked list and swap every two adjacent nodes — not by swapping their values, but by actually rewiring the pointers between them.
This distinction matters. Value swapping is trivial, but pointer manipulation is the real skill that interviewers want to see. The same rewiring pattern shows up in harder problems like Reverse Nodes in K-Group (#25) and Reverse Linked List II (#92).
Whether you prefer an iterative approach with a dummy node or a clean recursive solution, swap nodes in pairs leetcode teaches you the foundational mechanics of in-place linked list surgery. Let us walk through both approaches step by step.
Understanding the Swap Nodes in Pairs Problem
Given the head of a linked list, swap every two adjacent nodes and return the modified list. For example, given [1, 2, 3, 4], the output is [2, 1, 4, 3]. Given [1, 2, 3], the output is [2, 1, 3] — the last node stays in place because it has no pair.
The critical constraint is that you must not modify the values in the list nodes. Only the node references (next pointers) may be changed. This eliminates the obvious shortcut of just swapping node.val between pairs.
This problem is rated Medium on LeetCode and appears frequently in interviews at companies like Google, Amazon, and Microsoft. It tests your ability to carefully manipulate pointers without losing references — a skill that separates confident linked list solvers from those who get tangled in null pointer errors.
Iterative Approach: Dummy Node and Three-Pointer Swap
The iterative approach is the most intuitive way to solve swap pairs linked list. The key insight is to use a dummy node before the head so you never have to special-case the first pair. A prev pointer starts at this dummy and advances through the list two nodes at a time.
For each pair, you identify first (prev.next) and second (prev.next.next). Then you rewire exactly three pointers: prev.next points to second, first.next points to second.next, and second.next points to first. After the swap, prev advances to first — which is now the second node in the swapped pair.
This gives you O(n) time complexity with O(1) extra space. The dummy node trick is worth memorizing because it applies to dozens of linked list problems where the head might change.
- Create a dummy node pointing to head — this handles the edge case where head itself gets swapped
- Initialize prev = dummy, then loop while prev.next and prev.next.next both exist
- Set first = prev.next, second = prev.next.next
- Rewire: prev.next = second, first.next = second.next, second.next = first
- Advance prev to first (the node that is now in the second position)
- Return dummy.next as the new head
Three-Pointer Pattern
Use a dummy node and rewire 3 pointers per swap: prev.next = second, first.next = second.next, second.next = first. Then advance prev to first (which is now after second).
Recursive Approach: Elegant but Stack-Heavy
The recursive solution for swap nodes in pairs is beautifully concise. The idea is simple: if there are at least two nodes, swap the first pair and recursively solve the rest. The base case returns head when there are fewer than two nodes left.
In code, you set first = head and second = head.next. Then first.next = recursiveSwap(second.next), and second.next = first. Return second as the new head of this portion. Each recursive call handles one pair and delegates the rest.
The elegance comes at a cost: O(n) call stack space, since each pair adds a frame. For most interview problems this is acceptable, but the iterative approach is preferred when the interviewer asks about space optimization. Knowing both solutions shows depth.
Visual Walkthrough: Swap Nodes in Pairs Step by Step
Let us trace through the iterative approach with input [1, 2, 3, 4]. We start with dummy -> 1 -> 2 -> 3 -> 4, and prev points at dummy.
First iteration: first = 1, second = 2. We rewire prev.next = 2, 1.next = 3, 2.next = 1. The list is now dummy -> 2 -> 1 -> 3 -> 4. We advance prev to node 1.
Second iteration: first = 3, second = 4. We rewire prev.next = 4, 3.next = null, 4.next = 3. The list is now dummy -> 2 -> 1 -> 4 -> 3. We advance prev to node 3.
Now prev.next is null, so the loop ends. We return dummy.next, which is node 2. Final result: [2, 1, 4, 3]. Each swap touched exactly three pointers and nothing else.
Values vs. Pointers
You must NOT modify node values — the problem requires actual node swapping via pointer manipulation. Swapping values works but violates the constraint.
Edge Cases and Complexity Analysis
Edge cases for pairwise swap are straightforward but easy to miss in an interview if you do not think about them upfront. An empty list (null head) returns null immediately. A single node has no pair, so it returns unchanged.
For a list with two nodes, the swap happens exactly once. For odd-length lists like [1, 2, 3], the last node stays in place — the loop condition (prev.next and prev.next.next) naturally handles this by stopping when there is no complete pair.
Time complexity is O(n) for both iterative and recursive approaches — you visit each node exactly once. Space complexity is O(1) for iterative (just a few pointers) and O(n/2) = O(n) for recursive (call stack frames, one per pair).
- Empty list: return null — no nodes to swap
- Single node: return head unchanged — no pair exists
- Two nodes: one swap, return the second node as new head
- Odd length: last node remains in place, no special handling needed
- Even length: all nodes get swapped in pairs
What Swap Nodes in Pairs Teaches You
Swap adjacent nodes is a gateway problem for in-place linked list pointer rewiring. The three-pointer swap pattern — prev.next, first.next, second.next — is the exact same mechanic used in harder problems. Reverse Nodes in K-Group (#25) generalizes this to K nodes. Reverse Linked List II (#92) applies it to a subrange.
The dummy node technique is another transferable skill. Any time the head of a linked list might change during manipulation, a dummy node before head simplifies your code and eliminates edge cases. You will use this in Remove Nth Node From End (#19), Merge Two Sorted Lists (#21), and many others.
If you found this leetcode 24 solution walkthrough helpful, practice the pattern with YeetCode flashcards. Drilling the three-pointer swap until it becomes automatic means you will never fumble linked list manipulation in an interview again.
Stepping Stone
Swap Nodes in Pairs (#24) is the stepping stone to Reverse Nodes in K-Group (#25, a Hard) — the pair swap is a specific case of K-group reversal with K=2.