首页 > 其他 > 详细

Rotate List

时间:2014-02-17 17:24:57      阅读:225      评论:0      收藏:0      [点我收藏+]

Given a list, rotate the list to the right by k places, where k is non-negative.

For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

bubuko.com,布布扣
 1 public class Solution {
 2     public ListNode rotateRight(ListNode head, int n) {
 3         if(head==null || n==0) return head;
 4         int len = 1;
 5         ListNode p = head;
 6         while(p.next!=null){
 7             p = p.next;
 8             len++;
 9         }
10         p.next = head;
11         for(int i=0;i<len -n%len;i++){
12             p = p.next;
13         }
14         head = p.next;
15         p.next= null;
16         return head;
17     }
18 }
View Code

Rotate List

原文:http://www.cnblogs.com/krunning/p/3552069.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!