Swap Nodes in Pairs
MediumLinked List
Decode it first. Clues, constraints, pattern and plan before any code.You get the head of a linked list. Swap every two adjacent nodes: the 1st with the 2nd, the 3rd with the 4th, and so on, and return the new head. A last node without a partner stays where it is. Change the links, not the values stored in the nodes.
1 -> 2 -> 3 -> 4 → 2 -> 1 -> 4 -> 3
1 -> 2 -> 3 → 2 -> 1 -> 3
(empty) → (empty)Constraints
0 <= number of nodes <= 1000 <= Node.val <= 100