Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given 1->2->3->4->5->NULL
and k = 2
,
return 4->5->1->2->3->NULL
.
ListNode *rotateRight(ListNode *head, int k)
{
int length=1;
if(head ==NULL)
return NULL;
ListNode * p =head;
while(p->next)
{
p =p->next;
length++;
}
if(length ==k)
{
return head;
}
if(length ==1)
{
return head;
}
p->next =head;
p=head;
int temp ;
temp =length -k%length;
while(--temp)
{
p=p->next;
}
head= p->next;
p->next=NULL;
return head;
}