Problem
Rotate a singly linked list to the right by k positions and return its resulting head. One right rotation moves the last node to the front. Preserve the list order induced by those rotations.
The source uses ListNode, with int val and ListNode* next fields:
ListNode* rotateRight(ListNode* head, int k);
Square brackets in the examples display node values along the chain; they do not replace the pointer interface.
Examples
Input: head = [9,2,7,4], k = 6
Output list: [7,4,9,2]
Six right rotations have the same effect as two for this four-node list.
Input: head = [5,8], k = 0
Output list: [5,8]
Constraints
-
The list contains between 0 and 500 nodes.
-
Node values are between -100 and 100, inclusive.
-
0 <= k <= 2000000000
.
-
Return a list head, not a flattened value array.