Swap Nodes in Pairs

Easy

Question

Given a linked list, swap every pair of nodes and return its head.

Swap Nodes in Pairs Example 1

Input: head = [1 -> 2 -> 3 -> 4]

Swap Nodes in Pairs Example 1

Output: [2 -> 1 -> 4 -> 3]

Swap Nodes in Pairs Example 2

Input: head = [4 -> 6 -> 8]

Swap Nodes in Pairs Example 2

Output: [6 -> 4 -> 8]

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

What should the resulting linked list look like after swapping its pairs of nodes? head = [0 -> 5 -> 10 -> 20 -> 35]
[0 -> 10 -> 20 -> 5 -> 35]
[0 -> 10 -> 5 -> 35 -> 20]
[5 -> 0 -> 10 -> 35 -> 20]
[5 -> 0 -> 20 -> 10 -> 35]

Login or signup to save your code.

Notes