Rotate List
Medium
Question
Given the head of a linked list and an integer k
, rotate the list to the right by k
places.
Input: head = [1 -> 2 -> 3], k = 4
Output: [3 -> 1 -> 2]
Input: head = [1 -> 2 -> 3 -> 4 -> 5], k = 2
Output: [4 -> 5 -> 1 -> 2 -> 3]
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
What is the resulting list after rotating it by 5 places? head = [0 -> 2 -> 4]
[0 -> 2 -> 4]
[2 -> 4 -> 0]
[4 -> 0 -> 2]
[4 -> 2 -> 0]
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.