2Brute2Best
🔥0

Swap Nodes in Pairs

Medium·LeetCode #24Basic Anchor
LeetCode
30-Second Mental ModelLinked List — Reversal (Groups of k, k = 2)
Complexity:O(N)→O(N)
Key Insight

Walk the list one pair at a time, with prev just before the pair. For the pair a → b, change three arrows: prev → b, a → (b's old next), and b → a. Then move prev to a. A dummy node in front gives the first pair a prev too. Only pointers change, so the nodes and their values stay the same.

Core Validity Invariant

Everything before prev is already swapped, and the pair at prev->next is swapped only when both of its nodes exist.

01

Problem

You are given the head of a singly linked list. Swap every two neighbouring nodes, and return the head of the new list.

Swap the nodes, not the values. Each node keeps its own value, so only the next arrows may change. If the list has an odd number of nodes, the last node has no partner, so it stays where it is.

Input

head, the first node of a singly linked list. The list can be empty.

Output

The head of the list after the pairs are swapped.

Example 1
Input: head = [1, 2, 3, 4]
Output: [2, 1, 4, 3]

The pairs (1, 2) and (3, 4) swap places.

Example 2
Input: head = [1, 2, 3]
Output: [2, 1, 3]

The pair (1, 2) swaps. Node 3 has no partner, so it stays at the end.

Example 3
Input: head = [1]
Output: [1]

One node has no partner, so nothing changes.

Constraints
  • • The list has between 0 and 100 nodes.
  • • 0 ≤ Node.val ≤ 100
  • • Do not change any node's value. Only the `next` arrows may change.
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.

  1. 1.

    If I swap the first pair, which node becomes the new head?

  2. 2.

    After I swap a pair, which node has to point to the first node of the next pair?

  3. 3.

    The first pair has no node in front of it. How can I avoid writing a special case for it?

  4. 4.

    Which next arrow do I overwrite first, and do I still need its old value afterwards?

  5. 5.

    What should happen when only one node is left, or when the list is empty?

03

Brute force approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForcePut the node pointers in a vector, swap neighbours there, then relink in the new orderO(N)O(N)
OptimalWalk pairs with prev, a and b, and relink the three arrows in the right orderO(N)O(1)
09

Why does this belong to this pattern?

Clues in the problem statement
  • “swap every two adjacent nodes”Each block of 2 nodes is reversed on its own, and the blocks keep their order → Groups of k with k = 2.
  • “singly linked list”We can't look back, so keep a `prev` pointer to the node before the current block.
  • “swap nodes, not values”Only relink the arrows. Nodes move because their `next` pointers change, not because their data is copied.
Blocks of k = 2+reverse each block in place+prev → block head, block tail → next block→Linked List — Reversal (Groups of k, k = 2)

A pair is a block of size 2. Reversing a block of 2 means swapping its two nodes, which is exactly the three-arrow change. The node before the pair is the prev pointer, and it must be joined to the new head of the block, which is b. This is the easiest group because the block size is fixed at 2, so there is no counting ahead. The only check is that two nodes exist. Groups of k must first count k nodes, and decide what to do with a short last block.

10

Don't make this mistake

Reversing the whole list (pattern confusion)
✗ return reverse(head); // 1 → 2 → 3 → 4 becomes 4 → 3 → 2 → 1
✓ Swap each pair only: 1 2 3 4 becomes 2 1 4 3.

This is the pattern-confusion trap: full reversal flips every arrow in the list, but this question only swaps each pair. On the dry run [1, 2, 3, 4, 5], reversing everything gives 5 → 4 → 3 → 2 → 1. The answer is 2 → 1 → 4 → 3 → 5, where 3 pairs with 4, not with 1.

Swapping the values instead of the nodes
✗ swap(a->val, b->val); // the numbers move, the nodes stay where they are
✓ prev->next = b; a->next = b->next; b->next = a;

The printed list looks right, but the node objects did not move. The statement only lets the next arrows change, and the test checks the node identities, so this fails. Relink the nodes instead.

Losing the rest of the list in the middle of the three arrows
✗ prev->next = b; b->next = a; a->next = b->next; // b->next is already a, so a->next = a
✓ a->next = b->next; // read b->next first b->next = a; prev->next = b;

Order matters. After b->next = a, the node b no longer knows the next pair. Now b->next is a, so a->next = b->next sets a->next = a. The node points at itself, so the rest of the list is lost and any walk loops forever. On [1, 2, 3, 4, 5] the walk from the dummy becomes 2 → 1 → 1 → 1 … forever. Read b->next before you overwrite it.

Not checking for a full pair (the odd node)
✗ while (prev->next != nullptr) { // a = prev->next; b = a->next; ... // b is null when the count is odd, so b->next crashes
✓ while (prev->next != nullptr && prev->next->next != nullptr) { ... }

With an odd number of nodes, the last node has no partner. If the loop only checks prev->next, then b is null and b->next crashes. Check that both nodes of the pair exist before touching them.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Linked List — Reversal translate into production architectures and low-level memory mechanics.