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
.
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 }
原文:http://www.cnblogs.com/krunning/p/3552069.html