Rearrange a given linked list in-place in Zig Zag
PROBLEM :
Given a singly linked list L0 -> L1 -> … -> Ln-1 -> Ln. Rearrange the nodes in the list so that the new formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2.
Input:
You have to complete the method which takes 1 argument: the head of the linked list. You should not read any input from stdin/console. There are multiple test cases. For each test case, this method will be called individually.
Output:
Your function should return a pointer to the rearranged list so obtained.
Constraints:
1 <=T<= 50
1 <= size of linked lists <= 100
Example:
Input:
2
4
1 2 3 4
5
1 2 3 4 5
Output:
1 4 2 3
1 5 2 4 3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
The structure of linked list is the following
struct Node
{
int data;
Node* next;
};
*/
struct Node* reverse(struct Node *) ;
Node *inPlace(Node *root)
{
if(root==NULL||root->next==NULL)
return root ;
Node *l1,*t1,*t2,*l2 ;
t1=root ;
t2=t1->next ;
l1=root ;
while(t2!=NULL&&t2->next!=NULL)
{
t1=t1->next ;
t2=t2->next->next ;
}
l2=t1->next ;
t1->next=NULL ;
l2=reverse(l2) ;
t1=l1;t2=l2 ;
while(l1!=NULL&&l2!=NULL)
{
t1=l2->next ;
l2->next=l1->next ;
l1->next=l2 ;
l1=l1->next->next ;
l2=t1;
}
return root ;
}
struct Node* reverse(struct Node *head)
{
if(head==NULL)
return head ;
if(head->next==NULL)
return head;
struct Node *one,*two,*third ;
one=head ;
two=one->next ;
third=two->next ;
one->next=NULL ;
two->next=one ;
while(third!=NULL)
{
one=two ;
two=third ;
third=third->next ;
two->next=one ;
}
return two ;
}
---------------------------------------------------------------------------------
Given a singly linked list L0 -> L1 -> … -> Ln-1 -> Ln. Rearrange the nodes in the list so that the new formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2.
Input:
You have to complete the method which takes 1 argument: the head of the linked list. You should not read any input from stdin/console. There are multiple test cases. For each test case, this method will be called individually.
Output:
Your function should return a pointer to the rearranged list so obtained.
Constraints:
1 <=T<= 50
1 <= size of linked lists <= 100
Example:
Input:
2
4
1 2 3 4
5
1 2 3 4 5
Output:
1 4 2 3
1 5 2 4 3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
The structure of linked list is the following
struct Node
{
int data;
Node* next;
};
*/
struct Node* reverse(struct Node *) ;
Node *inPlace(Node *root)
{
if(root==NULL||root->next==NULL)
return root ;
Node *l1,*t1,*t2,*l2 ;
t1=root ;
t2=t1->next ;
l1=root ;
while(t2!=NULL&&t2->next!=NULL)
{
t1=t1->next ;
t2=t2->next->next ;
}
l2=t1->next ;
t1->next=NULL ;
l2=reverse(l2) ;
t1=l1;t2=l2 ;
while(l1!=NULL&&l2!=NULL)
{
t1=l2->next ;
l2->next=l1->next ;
l1->next=l2 ;
l1=l1->next->next ;
l2=t1;
}
return root ;
}
struct Node* reverse(struct Node *head)
{
if(head==NULL)
return head ;
if(head->next==NULL)
return head;
struct Node *one,*two,*third ;
one=head ;
two=one->next ;
third=two->next ;
one->next=NULL ;
two->next=one ;
while(third!=NULL)
{
one=two ;
two=third ;
third=third->next ;
two->next=one ;
}
return two ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment