AlgoMaster Logo

Swap Nodes in Pairs

mediumFrequency5 min readUpdated June 23, 2026

Understanding the Problem

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 constraint that matters is that we cannot swap values. We have to rewire the node pointers. Swapping two adjacent nodes in a singly linked list requires updating three pointers: the two nodes in the pair, plus the node that points into the pair. Miss the third one and the chain breaks.

For each pair, then, we need to track three nodes: the node before the pair (so we 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.

Key Constraints:

  • 0 <= n <= 100 → The list can be empty or have a single node, so both approaches must handle a list with fewer than two nodes without touching any pointers.
  • 0 <= Node.val <= 100 → The values do not affect the algorithm, since we rearrange nodes rather than read their values.
  • Cannot modify node values → This rules out the shortcut of swapping .val between adjacent nodes. We have to rearrange the nodes themselves, which is what makes the pointer work necessary.

Approach 1: Recursive

Intuition

Swapping the whole list reduces to swapping one pair plus swapping everything after it. If we have at least two nodes, we swap the first two, then recursively swap the rest of the list starting at the third node. The base case is fewer than two nodes remaining, where there is nothing to swap.

The connection between the two halves is the link out of the pair. After swapping nodes 1 and 2, node 1 ends up in the second position, so its next should point to the head returned by recursively swapping the rest of the list. Because each recursive call returns the new head of its swapped sublist, attaching that result to node 1 stitches the pairs together correctly.

Algorithm

  1. If the list is empty or has only one node, return the head as-is (base case).
  2. Let first = head and second = head.next.
  3. Recursively swap the sublist starting from second.next (the third node onward).
  4. Set first.next to the result of the recursive call.
  5. Set second.next to first.
  6. Return second as the new head of this swapped pair.

Example Walkthrough

1Initial: first=1, second=2
first
1
2
second
3
4
null
1/4

Code

The recursion is correct, but it carries O(n) stack space. The next approach does the same rewiring with a single pointer walking the list, which brings the space down to O(1).

Approach 2: Iterative with Dummy Node

Intuition

The iterative approach processes pairs from left to right using a pointer that tracks the node immediately before each pair. A dummy node pointing to the head serves as the "previous" node for the first pair, so the head no longer needs special handling: when the first pair swaps, dummy.next updates to the new head along with every other rewiring.

For each pair, we do three pointer rewirings:

  1. The previous node's next should point to the second node (which becomes the new first in the pair).
  2. The first node's next should point to whatever comes after the pair.
  3. The second node's next should point to the first node.

After the swap, the first node sits in the second position of the pair, so it becomes the "previous" node for the next pair. Advancing prev to the second node instead would skip the swap of the following pair, so the pointer has to land on the first node.

Algorithm

  1. Create a dummy node with dummy.next = head.
  2. Initialize prev = dummy.
  3. While prev.next and prev.next.next are not null (there's a complete pair):
    • Let first = prev.next and second = prev.next.next.
    • Rewire: prev.next = second, first.next = second.next, second.next = first.
    • Advance: prev = first (first is now in the second position of the swapped pair).
  4. Return dummy.next.

Example Walkthrough

1Initialize: dummy → 1 → 2 → 3 → 4, prev = dummy
first
1
2
second
3
4
null
1/4

Code