原题链接:Leecode 61. 旋转链表


/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(head==nullptr) return nullptr;
if(!k) return head;
ListNode* root=head;
ListNode* pre=head;
int num=1;
while(head->next)
{
num++;
pre=head;
head=head->next;
}
k%=num;
if(!k) return root;
head->next=root;
pre->next=nullptr;
return rotateRight(head,--k);
}
};










