Problem 122
Swap Nodes in Pairs
Medium · 20 pts · Linked List, Recursion
Problem
You are given a linked list. Swap every two adjacent nodes: the 1st with the 2nd, the 3rd with the 4th, and so on, and print the result. Swap the nodes themselves by changing links, not just the values. If the list has an odd length, the last node has no partner and stays in place. Use a dummy node before the head. For each pair (a, b) after the current position, relink as previous → b → a → rest and move to a.
Input
First line: integer N, the number of nodes. Second line: the N node values from head to tail.
Output
Print the node values of the resulting list from head to tail, space-separated, or EMPTY if it has no nodes.
Constraints
0 <= N <= 100; 0 <= value <= 100
Example input
4 1 2 3 4
Example output
2 1 4 3
Explanation
In 1 2 3 4, the pairs are (1, 2) and (3, 4). Swapping each pair gives 2 1 4 3.