JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 1 public class Solution {
 2     public ListNode rotateRight(ListNode head, int n) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         if(head == null || n == 0){
 6             return head;
 7         }
 8         int len = 1;
 9         ListNode p = head;
10         while(p.next != null){
11             len ++;
12             p = p.next;
13         }
14         p.next = head;
15 
16         int count = (len - n) % len;
17         while(count < 0){
18             count += len;
19             count %= len;
20         }
21         int step = 0;
22         while(step < count){
23             p = p.next;
24             step ++;
25         }
26         head = p.next;
27         p.next = null;
28         return head;
29     }
30 }

 

posted on 2013-11-20 09:15  JasonChang  阅读(120)  评论(0编辑  收藏  举报