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]
Take a moment to understand the problem and think of your approach before you start coding.