class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head==null||head.next==null){
return head;
}
ListNode tail=head;
int length=1;
while(tail.next!=null){
tail=tail.next;
length++;
}
tail.next=head;
int step=k%length;
tail=head;
for(int i=0;i<length-step-1;i++){
tail=tail.next;
}
head=tail.next;
tail.next=null;
return head;
}
}
LeetCode精选100题——第61题——旋转链表
最新推荐文章于 2022-01-17 19:34:51 发布