Swap Nodes in Pairs
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.
Everything before prev is already swapped, and the pair at prev->next is swapped only when both of its nodes exist.
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.
head, the first node of a singly linked list. The list can be empty.
The head of the list after the pairs are swapped.
The pairs (1, 2) and (3, 4) swap places.
The pair (1, 2) swaps. Node 3 has no partner, so it stays at the end.
One node has no partner, so nothing changes.
- • 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.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
If I swap the first pair, which node becomes the new head?
- 2.
After I swap a pair, which node has to point to the first node of the next pair?
- 3.
The first pair has no node in front of it. How can I avoid writing a special case for it?
- 4.
Which
nextarrow do I overwrite first, and do I still need its old value afterwards? - 5.
What should happen when only one node is left, or when the list is empty?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Put the node pointers in a vector, swap neighbours there, then relink in the new order | O(N) | O(N) |
| Optimal | Walk pairs with prev, a and b, and relink the three arrows in the right order | O(N) | O(1) |
Why does this belong to this pattern?
- “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.
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.
Don't make this mistake
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.
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.
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.
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.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Linked List — Reversal translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.