139. Rotate List
Medium · Linked List
Given the head of a linked list, rotate the list to the right by k places.
Rotating a list to the right by k places means taking the last k elements and moving them to the front. For example, rotating [1,2,3,4,5] right by 2 gives [4,5,1,2,3].
If k is greater than the length of the list, treat it as k modulo the list length.
Examples
Example 1 Input: head = [1,2,3,4,5], k = 2 Output: [4,5,1,2,3] Explanation: Rotate right by 2: take the last 2 elements [4,5] and move to front.
Example 2 Input: head = [0,1,2], k = 4 Output: [2,0,1] Explanation: List length is 3, so k=4 is equivalent to k=1. Rotate right by 1: [2,0,1].
Constraints
- Standard input/output constraints apply