Loading content...
You are given the head of a singly linked list. Your task is to exchange every pair of consecutive nodes in the list and return the modified list's head.
Important Constraint: You must accomplish this by rearranging the actual node references in the list. Directly modifying the values stored within the nodes is not permitted—only the pointer connections between nodes may be altered.
For example, if the list contains nodes with values [A, B, C, D], after the operation, the list should represent [B, A, D, C]. The nodes themselves are swapped, not just their data.
If the list has an odd number of nodes, the final node remains in its original position since it has no adjacent partner to swap with.
head = [1,2,3,4][2,1,4,3]The first pair (1,2) is swapped to become (2,1), and the second pair (3,4) is swapped to become (4,3). The resulting list is [2,1,4,3].
head = [][]An empty list has no nodes to swap, so the result is also an empty list.
head = [1,2,3][2,1,3]The first pair (1,2) is swapped to become (2,1). The third node (3) has no adjacent partner, so it remains at the end. Result: [2,1,3].
Constraints