We need to take every consecutive pair of nodes in a linked list and swap their positions. So node 1 and node 2 swap, node 3 and node 4 swap, and so on. If the list has an odd number of nodes, the last node stays where it is.
The constraint that matters is that we cannot swap values. We have to rewire the node pointers. Swapping two adjacent nodes in a singly linked list requires updating three pointers: the two nodes in the pair, plus the node that points into the pair. Miss the third one and the chain breaks.
For each pair, then, we need to track three nodes: the node before the pair (so we can rewire its next), the first node in the pair, and the second node in the pair. A dummy node before the head simplifies the first swap, since the head itself has no predecessor.
0 <= n <= 100 → The list can be empty or have a single node, so both approaches must handle a list with fewer than two nodes without touching any pointers.0 <= Node.val <= 100 → The values do not affect the algorithm, since we rearrange nodes rather than read their values.Cannot modify node values → This rules out the shortcut of swapping .val between adjacent nodes. We have to rearrange the nodes themselves, which is what makes the pointer work necessary.Swapping the whole list reduces to swapping one pair plus swapping everything after it. If we have at least two nodes, we swap the first two, then recursively swap the rest of the list starting at the third node. The base case is fewer than two nodes remaining, where there is nothing to swap.
The connection between the two halves is the link out of the pair. After swapping nodes 1 and 2, node 1 ends up in the second position, so its next should point to the head returned by recursively swapping the rest of the list. Because each recursive call returns the new head of its swapped sublist, attaching that result to node 1 stitches the pairs together correctly.
first = head and second = head.next.second.next (the third node onward).first.next to the result of the recursive call.second.next to first.second as the new head of this swapped pair.The recursion is correct, but it carries O(n) stack space. The next approach does the same rewiring with a single pointer walking the list, which brings the space down to O(1).
The iterative approach processes pairs from left to right using a pointer that tracks the node immediately before each pair. A dummy node pointing to the head serves as the "previous" node for the first pair, so the head no longer needs special handling: when the first pair swaps, dummy.next updates to the new head along with every other rewiring.
For each pair, we do three pointer rewirings:
next should point to the second node (which becomes the new first in the pair).next should point to whatever comes after the pair.next should point to the first node.After the swap, the first node sits in the second position of the pair, so it becomes the "previous" node for the next pair. Advancing prev to the second node instead would skip the swap of the following pair, so the pointer has to land on the first node.
Before any rewiring, first and second both hold live references, so we can read second.next (the node after the pair) at any point. The order used here, prev.next = second then first.next = second.next then second.next = first, keeps that read valid: first.next = second.next runs while second.next still points past the pair, and only the final line overwrites it. Reordering so that second.next = first runs before we read second.next would lose the reference to the rest of the list.
dummy.next = head.prev = dummy.prev.next and prev.next.next are not null (there's a complete pair):first = prev.next and second = prev.next.next.prev.next = second, first.next = second.next, second.next = first.prev = first (first is now in the second position of the swapped pair).dummy.next.dummy, prev, first, second). No recursion stack, no extra data structures.