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.
The Buddy Swap

Swapping nodes in pairs is a lesson in "keeping your finger on the page." When you swap two nodes, you have to rewire three different pointers: the one before the pair, the one between them, and the one after them.

Double-Link Re-snapping

We use a Dummy Node to handle the head swap and a prev pointer to stay one step behind our swap target. In each loop, we identify the two buddies (first and second) and perform a choreographed re-linking.

python
dummy = Node(0, head)
prev = dummy

while prev.next and prev.next.next:
    # 1. Identify Buddies
    first = prev.next
    second = first.next
    
    # 2. Re-wire (The Swap)
    prev.next = second
    first.next = second.next
    second.next = first
    
    # 3. Jump forward 2 steps
    prev = first
Why It's O(1) Space

We only use a handful of temporary pointers regardless of how long the list is. We are simply moving existing physical nodes around in memory like pieces on a chessboard.

Worked Example:[1, 2, 3, 4]
1
first
2
second
3
4
NULL
We identify the first pair: first is 1, second is 2. We will rewire pointers to swap them.
2
1
prev
3
4
NULL
We rewire the link: dummy points to 2, 1 points to 3, and 2 points to 1. We move 'prev' to 1 (the tail of the swapped pair).
2
1
4
3
NULL
We swap the next pair: 1 points to 4, 3 points to null, and 4 points to 3, yielding [2, 1, 4, 3].
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