/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(head == NULL || head->next ==NULL || k==0)
return head;
// 统计链表结点数
ListNode* l=head;
int count=0;
while(l)
{
count++;
l = l->next;
}
// 将单链表变为循环链表
ListNode* l1=head;
for(int i=1;i<count;i++)
l1 = l1->next;
l1->next = head;
// 循环
if(k<count)
k=k;
else
while(k>count)
k -= count;
int num = count-k;
while(num)
{
head=head->next;
num--;
}
ListNode* l2 = head;
for(int i=1;i<count;i++)
l2=l2->next;
l2->next = NULL;
return head;
}
};