题目描述
AC代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head==null)
return head;
ListNode first=head,second=head;
int n=0;//求长度
for(ListNode p=head;p!=null;p=p.next)
n++;
k%=n;
//双指针
while(k>0){
k--;
first=first.next;
}
while(first.next!=null){
first=first.next;
second=second.next;
}
first.next=head;
head=second.next;
second.next=null;
return head;
}
}