Rotate a Linked List by K values
PROBLEM :
Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer smaller than or equal to lenght of the linked list. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40.
Note: Function may assume that k is smaller than the count of nodes in linked list.
Input:
In this problem, complete the method which takes two argument: the head of the linked list and int k. We should not read any input from stdin/console.
The struct Node has a data part which stores the data and a next pointer which points to the next element of the linked list.
There are multiple test cases. For each test case, this method will be called individually.
Output:
Rotate the link list from index k and return its new head pointer.
Note: If you use "Test" or "Expected Output Button" use below example format
Example:
Input:
1
8
1 2 3 4 5 6 7 8
4
Output:
5 6 7 8 1 2 3 4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
Rotate a linked list after node k
The input list will have at least one element
Return pointer to head of rotated linked list
Node is defined as
struct node
{
int data;
struct node *next;
}
*/
void rotate(struct node **head_ref, int k)
{
if((*head_ref)==NULL||k==0)
return ;
struct node *temp,*ptr;
temp=(*head_ref) ;
while(k!=1&&temp!=NULL)
{
temp=temp->next ;
k-- ;
}
if(temp==NULL)
return ;
ptr=(*head_ref) ;
while(ptr->next!=NULL)
ptr=ptr->next ;
ptr->next=(*head_ref) ;
(*head_ref)=temp->next ;
temp->next=NULL ;
}
---------------------------------------------------------------------------------
Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer smaller than or equal to lenght of the linked list. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40.
Note: Function may assume that k is smaller than the count of nodes in linked list.
Input:
In this problem, complete the method which takes two argument: the head of the linked list and int k. We should not read any input from stdin/console.
The struct Node has a data part which stores the data and a next pointer which points to the next element of the linked list.
There are multiple test cases. For each test case, this method will be called individually.
Output:
Rotate the link list from index k and return its new head pointer.
Note: If you use "Test" or "Expected Output Button" use below example format
Example:
Input:
1
8
1 2 3 4 5 6 7 8
4
Output:
5 6 7 8 1 2 3 4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
Rotate a linked list after node k
The input list will have at least one element
Return pointer to head of rotated linked list
Node is defined as
struct node
{
int data;
struct node *next;
}
*/
void rotate(struct node **head_ref, int k)
{
if((*head_ref)==NULL||k==0)
return ;
struct node *temp,*ptr;
temp=(*head_ref) ;
while(k!=1&&temp!=NULL)
{
temp=temp->next ;
k-- ;
}
if(temp==NULL)
return ;
ptr=(*head_ref) ;
while(ptr->next!=NULL)
ptr=ptr->next ;
ptr->next=(*head_ref) ;
(*head_ref)=temp->next ;
temp->next=NULL ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment