单链表的操作,先连接起来,再进行移动。
/**
* 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) return head;
int n=1;
ListNode *p;
p=head;
while(p->next!=NULL){
p=p->next;
n++;
}
p->next=head;
k=k%n;
int m=n-k;
while(m>0){
p=p->next;
m--;
}
head=p->next;
p->next=NULL;
return head;
}
};