Swap Nodes in Pairs
Easy
Question
Given a linked list, swap every pair of nodes and return its head.
Input: head = [1 -> 2 -> 3 -> 4]
Output: [2 -> 1 -> 4 -> 3]
Input: head = [4 -> 6 -> 8]
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]
All test cases pass! 🎉
Time limit exceeded
InputExpected OutputActual Output
Standard OutputScroll down...
Login or signup to save your code.
Uh oh... looks like you don't yet have access.
Not sure what this unlocks? Check out a free pattern section.