341. Rotate Linked List by K Places

EasyLinked ListLinked ListRotationTwo Pointer

Given the head of a singly linked list and an integer k, rotate the list to the right by k places.

The last k nodes move to the front. If k >= n, use k = k % n. Return the new head.

Input: Head of a singly linked list and integer k (k >= 0).

Output: Head of the rotated linked list.

Examples

Example 1
Input: [1,2,3,4,5], 2
Output: [4,5,1,2,3]
Explanation: Rotate right 2: [4,5] move to front -> [4,5,1,2,3].
Example 2
Input: [0,1,2], 4
Output: [2,0,1]
Explanation: k=4, n=3. Effective k=1. [2] moves to front.
Example 3
Input: [1,2,3,4,5], 0
Output: [1,2,3,4,5]
Explanation: k=0. No rotation.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →