61. Rotate List

旋转链表

思路:

把链表连接为一个闭合的环,然后从头计数,到位置剪开

java:

 1 class Solution {
 2     public ListNode rotateRight(ListNode head, int k) {
 3         if(head==null||head.next==null) return head;
 4         ListNode copyhead = head;
 5         int size=1;
 6         while(copyhead.next!=null){
 7             copyhead=copyhead.next;
 8             size++;
 9         }
10         copyhead.next=head;
11         for(int i=size-k%size;i>1;i--){
12             head=head.next;
13         }
14         copyhead=head.next;
15         head.next=null;
16         return copyhead;
17     }
18 }
原文地址:https://www.cnblogs.com/fcyworld/p/7651809.html