24. Swap Nodes in Pairs

Practice Swap Nodes in Pairs for free on NudgeCode: Medium, Linked List pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

Walk a linked list two nodes at a time and swap each adjacent pair, then return the new head. Move the nodes themselves; rewriting the values is not allowed. If the list has an odd length, the last node stays where it is.

Example

head = [1,2,3,4]  ->  [2,1,4,3]

Constraints