Pairwise Swap Nodes in a Linked List
Solve this Problemprev pointer right before each pair with a dummy node, exactly the same idea used to reverse any run of nodes: relink first.next, second.next, and prev.next in a fixed three-line sequence so second ends up leading and first trails right behind it, then slide prev up to first — now the trailing node of the pair just swapped — before repeating for the next pair.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
0 ≤ number of nodes in head ≤ 200 - ◆
-1000 ≤ node value ≤ 1000
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public Node swapPairs(Node head) { |
| 3 | Node dummy = new Node(0); |
| 4 | dummy.next = head; |
| 5 | Node prev = dummy; |
| 6 | while (prev.next != null && prev.next.next != null) { |
| 7 | Node first = prev.next; |
| 8 | Node second = first.next; |
| 9 | first.next = second.next; |
| 10 | second.next = first; |
| 11 | prev.next = second; |
| 12 | prev = first; |
| 13 | } |
| 14 | return dummy.next; |
| 15 | } |
| 16 | } |
| 17 |
dummyA dummy node lets the very first pair be swapped the same way as every later one. prev starts at dummy.
Approach & Solutions
Brute Force — Collect Values, Swap Adjacent Pairs, Write Back
GoodCopy every value into an array, swap each adjacent pair of array entries, then walk the list once more overwriting .val in that swapped order. This never touches a single .next pointer — it's correct, but it costs an array the size of the whole list where the optimal solution needs none.
O(n)O(n)1// Node definition used in this problem:
2// class Node {
3// int val;
4// Node next;
5// }
6
7class Solution {
8 public Node swapPairs(Node head) {
9 List<Integer> vals = new ArrayList<>();
10 Node curr = head;
11 while (curr != null) {
12 vals.add(curr.val);
13 curr = curr.next;
14 }
15 int i = 0;
16 while (i + 1 < vals.size()) {
17 int temp = vals.get(i);
18 vals.set(i, vals.get(i + 1));
19 vals.set(i + 1, temp);
20 i += 2;
21 }
22 curr = head;
23 for (int v : vals) {
24 curr.val = v;
25 curr = curr.next;
26 }
27 return head;
28 }
29}Optimal — Swap Pairs by Relinking, Not by Value
OptimalA dummy node lets the very first pair swap the same way as every later one. For each pair: first is prev's next node, second is first's next. Relink so second comes before first — first.next skips over second, second.next points at first, and prev.next points at second — then move prev up to first, which is now the trailing node of the pair just swapped, ready to anchor the next one. No new nodes, no extra memory.
O(n)O(1)1// Node definition used in this problem:
2// class Node {
3// int val;
4// Node next;
5// }
6
7class Solution {
8 public Node swapPairs(Node head) {
9 Node dummy = new Node(0);
10 dummy.next = head;
11 Node prev = dummy;
12 while (prev.next != null && prev.next.next != null) {
13 Node first = prev.next;
14 Node second = first.next;
15 first.next = second.next;
16 second.next = first;
17 prev.next = second;
18 prev = first;
19 }
20 return dummy.next;
21 }
22}