C Write a method Node NoderRotate which leftrotates a list t
C++
Write a method Node *Node::rRotate() which left-rotates a list (the head node is moved to the tail).
C++
Write a method Node *Node::rRotate() which left-rotates a list (the head node is moved to the tail).
C++
Write a method Node *Node::rRotate() which left-rotates a list (the head node is moved to the tail).
Solution
#include<stdio.h>
int main(){
void rotate_list_left(listnode** head, int k)
{
if( !head || !*head )
{
printf( \"\ rotate_list_left: empty list = so return \ \" );
return;
}
if( k < 1 )
{
printf( \"\ rotate_list_left:invalid input: k must be >= 1 \ \" );
return;
}
listnode* post = *head;
listnode* curr = *head;
while(k--)
{
post = post->next;
if( !post )
{
printf( \"\ rotate_list_left:invalid input: k should be smaller than list size \ \" );
return;
}
}
while(post->next)
{
curr = curr->next;
post = post->next;
}
listnode* tmp = *head;
*head = curr->next;
curr->next = 0;
post->next = tmp;
return 0;
}
}
