Rotate List

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     ListNode *rotateRight(ListNode *head, int k) {
 2         // IMPORTANT: Please reset any member data you declared, as
 3         // the same Solution instance will be reused for each test case.
 4         if(head == NULL)
 5             return NULL;
 6         int l = 1;
 7         ListNode *tmp = head;
 8         while(tmp->next){
 9             tmp = tmp->next;
10             l++;
11         }
12         ListNode *end = tmp;
13         k = k%l;
14         if(k == 0)
15             return head;
16         int t = 1;
17         tmp = head;
18         while(tmp->next){
19             if(t == l-k)
20                 break;
21             t++;
22             tmp = tmp->next;
23         }
24         ListNode *result = tmp->next;
25         end->next = head;
26         tmp->next = NULL;
27         return result;
28     }
原文地址:https://www.cnblogs.com/waruzhi/p/3421451.html