Algorithm

Swap Nodes in Pairs

Linked List Pattern

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.

CONSTRAINTS
  • The number of nodes is in the range [0, 100]
  • 0 <= Node.val <= 100
  • Solve by rewiring pointers, not by modifying node values.
EXAMPLE 1
Input: head = [1,2,3,4]
Output: [2,1,4,3]
The pairs (1,2) and (3,4) each swap, so 2 precedes 1 and 4 precedes 3.
EXAMPLE 2
Input: head = [1,2,3]
Output: [2,1,3]
The pair (1,2) swaps to (2,1); node 3 has no partner, so it stays where it is.
EXAMPLE 3
Input: head = [1]
Output: [1]
A single node has no adjacent node to swap with, so the list is unchanged.
Can I just swap the values inside the two nodes?
That produces the right output but sidesteps the skill being tested — pointer rewiring. Many interviewers explicitly require you to relink the nodes, which is also what generalizes to problems where nodes can't be treated as interchangeable value holders.
What happens with an odd number of nodes?
The last unpaired node keeps its position. The loop simply stops when fewer than two nodes remain ahead.
How is this related to Reverse Nodes in k-Group?
It's that problem with k = 2 — reversing every block of two. The pair swap is the smallest, most explicit case of block reversal.

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.

What a single swap really touches

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 reframe: give every pair a predecessor

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.

python
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.next
Crucial Notethe three rewires must run in an order that never strands a node before you've re-secured it. Do first.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.
Worked Example:[1, 2, 3, 4]
- prev = dummy. first = 1, second = 2. Rewire: 1 → 3, 2 → 1, dummy → 2. List: 2 → 1 → 3 → 4. Advance prev to 1.
- prev = 1. first = 3, second = 4. Rewire: 3 → null, 4 → 3, 1 → 4. List: 2 → 1 → 4 → 3. Advance prev to 3.
- prev = 3. prev.next is null — fewer than two nodes ahead. Stop.
- Result: 2 → 1 → 4 → 3. (An odd tail, had one existed, would fail the guard and stay put.)
The cost, and the takeaway

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.

Interactive Strategy Visualization
PAIR RECOIL ENGINE

Structural Node Swapping Intelligence

1
2
3
4
READY AT DUMMY

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.

Mental Model

Think of it as a leapfrog movement. You bridge the gap between two nodes, swap them, then hop over them to the next pair.

Strategy: Pointer Redirection

Time Complexity: O(N) | Space Complexity: O(1). Pure link manipulation without swapping values.

O(N) Time · O(1) Space Three-Rewire Pair Swap