61. Rotate List

 1 class Solution {
 2     public ListNode rotateRight(ListNode head, int k) {
 3         if(head == null) return null;
 4         int n = 0;
 5         ListNode node1 = head;
 6         ListNode last = node1;
 7         while(node1 != null) {
 8             n++;
 9             if(node1.next == null) {
10                 last = node1;
11             }
12             node1 = node1.next;
13             
14         }
15         int times = k % n;
16         for(int i = 0; i < times; i++) {
17             node1 = head;
18             while(node1.next != last) {
19                 node1 = node1.next;
20             }
21             node1.next = null;
22             last.next = head;
23             head = last;
24             last = node1;
25             
26         }
27         return head;
28     }
29 }

 

posted @ 2018-09-13 13:21  jasoncool1  阅读(96)  评论(0编辑  收藏  举报