Swap every two adjacent nodes and return the new head: 1 -> 2 -> 3 -> 4 becomes 2 -> 1 -> 4 -> 3. If the length is odd the last node stays. Change the pointers, not the values stored in the nodes.
The tests show a list as a Python list of its values: [1, 2, 3] stands for 1 -> 2 -> 3.
Example 1
Input:
swap_pairs(head) # head = 1 -> 2 -> 3 -> 4
Output:
[2, 1, 4, 3]
Example 2
Input:
swap_pairs(head) # head = 1 -> 2 -> 3
Output:
[2, 1, 3]
0 <= number of nodes <= 100000-10**9 <= val <= 10**9No results yet
Run your code to check it against the examples, or submit to run every test.