Medium
Given the head
of a linked list, rotate the list to the right by k
places.
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, 500]
.-100 <= Node.val <= 100
0 <= k <= 2 * 109
using LeetCodeNet.Com_github_leetcode;
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode RotateRight(ListNode head, int k) {
if (head == null || head.next == null || k == 0) {
return head;
}
// Compute the length
int len = 1;
ListNode tail = head;
while (tail.next != null) {
tail = tail.next;
len++;
}
k = k % len;
if (k == 0) {
return head;
}
// Make it a circle
tail.next = head;
// Find new tail: (len - k - 1)th node
ListNode newTail = head;
for (int i = 0; i < len - k - 1; i++) {
newTail = newTail.next;
}
ListNode newHead = newTail.next;
newTail.next = null;
return newHead;
}
}