class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode * newhead = NULL;
ListNode * pre=NULL;
ListNode * temp = head;
int i=0;
int length=0;
if(head == NULL)
return NULL;
//find length
while(temp!=NULL)
{
length++;
temp=temp->next;
}
k=k%length;
if (k==0)
return head;
temp=head;
for(i=0; i<length-k;i++)
{
pre=temp;
temp=temp->next;
}
pre->next=NULL;
newhead=temp;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head;
return newhead;
}
};
public:
ListNode *rotateRight(ListNode *head, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode * newhead = NULL;
ListNode * pre=NULL;
ListNode * temp = head;
int i=0;
int length=0;
if(head == NULL)
return NULL;
//find length
while(temp!=NULL)
{
length++;
temp=temp->next;
}
k=k%length;
if (k==0)
return head;
temp=head;
for(i=0; i<length-k;i++)
{
pre=temp;
temp=temp->next;
}
pre->next=NULL;
newhead=temp;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head;
return newhead;
}
};