362. Rotate List by K Places

MediumLinked ListLinked ListTwo Pointers

Given the head of a linked list, rotate the list to the right by k places. k may be larger than the list length. The list is given as an array; return the rotated array. Input: '[list], k'.

Input: '[list], k'.

Output: Array — the rotated list.

Examples

Example 1
Input: [1,2,3,4,5], 2
Output: [4,5,1,2,3]
Explanation: Last two move to the front.
Example 2
Input: [0,1,2], 4
Output: [2,0,1]
Explanation: k mod n = 1.
Example 3
Input: [1,2], 0
Output: [1,2]
Explanation: No rotation.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →