注意:循环时需要取模
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head==null) return null;
ListNode p = head;
//circle
int i=0;
while(head.next!=null){
i++;
head = head.next;
}
i++; //total
head.next = p;
for(int h=0;h<i-k%i-1;h++){
p = p.next;
}
ListNode res = p.next;
p.next = null;
return res;
}
}