Description

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list nodes.

Examples

Input:head = [1,2,3,4]
Output:[2,1,4,3]
Explanation:

Swap pairs: (1,2) and (3,4).

Input:head = []
Output:[]
Explanation:

Edge case with empty result.

Input:head = [5,8,1]
Output:[8,5,1]
Explanation:

With an odd number of nodes, swap the first pair (5,8) to get (8,5), and leave the last node 1 unchanged since it has no pair.

Constraints

  • 0 ≤ list length ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!