Given a linked list, swap every two adjacent nodes and return its head. The values inside the nodes must not be modified — only node rearrangement is allowed.
Output: [2,1,4,3]
Explanation: Pairs (1,2) and (3,4) swapped.
Output: [1]
Use a dummy head. For each pair: link dummy→second, second→first, first→next pair start. Advance dummy by 2.
- dummy.next = second; second.next = first; first.next = next pair
- Advance: dummy = first (now behind next pair)
Complexity Analysis:
Time complexity: O(n)
Space complexity: O(1)