/**
* 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||k==0) return head;
int len=0;
ListNode *p=head;
while(p->next!=NULL)
{
len++;
p=p->next;
}
len++;
k%=len;
int s=len-k;
p->next=head;
for(int i=0;i<s;i++)
{
p=p->next;
}
head=p->next;
p->next=NULL;
return head;
}
};
* 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||k==0) return head;
int len=0;
ListNode *p=head;
while(p->next!=NULL)
{
len++;
p=p->next;
}
len++;
k%=len;
int s=len-k;
p->next=head;
for(int i=0;i<s;i++)
{
p=p->next;
}
head=p->next;
p->next=NULL;
return head;
}
};