Reverse a Linked List in groups of given size.
PROBLEM :
Given a linked list, write a function to reverse every k nodes (where k is an input to the function).If a linked list is given as 1->2->3->4->5->6->7->8->NULL and k = 3 then output will be 3->2->1->6->5->4->8->7->NULL.
Example:
Inputs: 1->2->3->4->5->6->7->8->NULL and k = 3
Output: 3->2->1->6->5->4->8->7->NULL.
Inputs: 1->2->3->4->5->6->7->8->NULL and k = 5
Output: 5->4->3->2->1->8->7->6->NULL
Input:
In this problem, method takes two argument: the head of the linked list and int k. You 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:
Reverse the linked list in the group of given size and return the reference of starting node(head) of the reversed Linked list .
Note: If you use "Test" or "Expected Output Button" use below example format
Example:
Input:
1
8
1 2 2 4 5 6 7 8
4
Output:
4 2 2 1 8 7 6 5
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
Reverse a linked list
The input list will have at least one element
Return the node which points to the head of the new LinkedList
Node is defined as
struct node
{
int data;
struct Node *next;
}
*/
struct node *reverse (struct node *head, int k)
{
if(head==NULL)
return head ;
if(head->next==NULL)
return head;
if(k==1)
return head ;
struct node *one,*two,*third ;
int i=2 ;
one=head ;
one=head ;
two=one->next ;
third=two->next ;
one->next=NULL ;
two->next=one ;
while(i!=k&&third!=NULL)
{
one=two ;
two=third ;
third=third->next ;
i++ ;
two->next=one ;
}
if(third!=NULL)
head->next=reverse(third,k) ;
return two ;
}
---------------------------------------------------------------------------------
Given a linked list, write a function to reverse every k nodes (where k is an input to the function).If a linked list is given as 1->2->3->4->5->6->7->8->NULL and k = 3 then output will be 3->2->1->6->5->4->8->7->NULL.
Example:
Inputs: 1->2->3->4->5->6->7->8->NULL and k = 3
Output: 3->2->1->6->5->4->8->7->NULL.
Inputs: 1->2->3->4->5->6->7->8->NULL and k = 5
Output: 5->4->3->2->1->8->7->6->NULL
Input:
In this problem, method takes two argument: the head of the linked list and int k. You 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:
Reverse the linked list in the group of given size and return the reference of starting node(head) of the reversed Linked list .
Note: If you use "Test" or "Expected Output Button" use below example format
Example:
Input:
1
8
1 2 2 4 5 6 7 8
4
Output:
4 2 2 1 8 7 6 5
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
Reverse a linked list
The input list will have at least one element
Return the node which points to the head of the new LinkedList
Node is defined as
struct node
{
int data;
struct Node *next;
}
*/
struct node *reverse (struct node *head, int k)
{
if(head==NULL)
return head ;
if(head->next==NULL)
return head;
if(k==1)
return head ;
struct node *one,*two,*third ;
int i=2 ;
one=head ;
one=head ;
two=one->next ;
third=two->next ;
one->next=NULL ;
two->next=one ;
while(i!=k&&third!=NULL)
{
one=two ;
two=third ;
third=third->next ;
i++ ;
two->next=one ;
}
if(third!=NULL)
head->next=reverse(third,k) ;
return two ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment