1、简单做法(勉强算双指针吧)
/**
* 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==NULL||head->next==NULL||k==0) return head;
int len=1,cnt;
ListNode* cru=head;
while(cru->next!=NULL)
{
cru=cru->next;
++len;
}
cru->next=head;
cnt=k%len;
if(cnt==0) cnt==len;
cru=head;
head=head->next;
for(int i=0;i<len-cnt-1;++i)
{
cru=cru->next;
head=head->next;
}
cru->next=NULL;
return head;
}
};