思路:把后(k%length)个节点接到头部
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null || k == 0) return head;
//辅助节点用来遍历长度
ListNode help = head;
int len = 1;
while(help.next != null){
help = help.next;
len++;
}
help.next = head;
//辅助节点指向断开处
ListNode breakpoint = head;
for(int i = 1; i < len - (k % len); i++){
breakpoint = breakpoint.next;
}
ListNode newHead = breakpoint.next;
breakpoint.next = null;
return newHead;
}
}