Rotate a list right
Mediumlinked listtwo pointers
Rotate the list to the right by k places: the last node moves to the front, k times. k may be larger than the list's length.
Example 1
Input: head = [1,2,3,4,5], k = 2 Output: [4,5,1,2,3]
Example 2
Input: head = [0,1,2], k = 4 Output: [2,0,1]
Constraints
- 0 ≤ number of nodes ≤ 500
- -100 ≤ Node.val ≤ 100
- 0 ≤ k ≤ 2·10⁹
Submit checks your solution against 9 tests, including hidden ones.
// Provided by the judge: keep these definitions
class ListNode {
constructor(val = 0, next = null) {
this.val = val
this.next = next
}
}
/**
* @param {ListNode|null} head
* @param {int} k
* @return {ListNode|null}
*/
function rotateRight(head, k) {
}