Linked List in Zig-Zag fashion (Greater Smaller Zig-Zag)
PROBLEM :
Given a Linked list, rearrange it such that converted list should be of the form a < b > c < d > e < f .. where a, b, c are consecutive data node of linked list and such that the order of linked list is sustained.
For example: In 11 15 20 5 10 we consider only 11 20 5 15 10 because it satisfies the above condition and the order of linked list. 5 20 11 15 10 is not considered as it does not follow the order of linked list.
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 zig zack linked list.
Constraints:
1 <=T<= 100
1 <= size of linked list(a) <= 1000
Example:
Input:
2
4
1 2 3 4
5
11 15 20 5 10
Output:
1 3 2 4
11 20 5 15 10
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
The structure of linked list is the following
struct Node
{
int data;
Node* next;
};
*/
void swap(Node *,Node *) ;
Node *zigZack(Node* head)
{
if(head==NULL)
return head ;
Node *curr ;
bool temp ;
curr=head ;
temp=true ;
while(curr->next!=NULL)
{
if(temp)
{
if(curr->data>curr->next->data)
swap(curr,curr->next) ;
}
else
{
if(curr->data<curr->next->data)
swap(curr,curr->next) ;
}
curr=curr->next ;
temp=!temp ;
}
return head ;
}
void swap(Node *first,Node *second)
{
int temp ;
temp=first->data ;
first->data=second->data ;
second->data=temp ;
}
---------------------------------------------------------------------------------
Given a Linked list, rearrange it such that converted list should be of the form a < b > c < d > e < f .. where a, b, c are consecutive data node of linked list and such that the order of linked list is sustained.
For example: In 11 15 20 5 10 we consider only 11 20 5 15 10 because it satisfies the above condition and the order of linked list. 5 20 11 15 10 is not considered as it does not follow the order of linked list.
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 zig zack linked list.
Constraints:
1 <=T<= 100
1 <= size of linked list(a) <= 1000
Example:
Input:
2
4
1 2 3 4
5
11 15 20 5 10
Output:
1 3 2 4
11 20 5 15 10
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
The structure of linked list is the following
struct Node
{
int data;
Node* next;
};
*/
void swap(Node *,Node *) ;
Node *zigZack(Node* head)
{
if(head==NULL)
return head ;
Node *curr ;
bool temp ;
curr=head ;
temp=true ;
while(curr->next!=NULL)
{
if(temp)
{
if(curr->data>curr->next->data)
swap(curr,curr->next) ;
}
else
{
if(curr->data<curr->next->data)
swap(curr,curr->next) ;
}
curr=curr->next ;
temp=!temp ;
}
return head ;
}
void swap(Node *first,Node *second)
{
int temp ;
temp=first->data ;
first->data=second->data ;
second->data=temp ;
}
---------------------------------------------------------------------------------
inancom-tsu Kevin Islam https://wakelet.com/wake/Eq0C80DiGZqv7ARoDV_mC
ReplyDeletehuntsymwatchdrum
probalgyra_1986 Nikki Marie link
ReplyDeleteclick
download
click here
clinlatabmarg
stiral0in_mi1981 Robert Ritchie 4K Video Downloader
ReplyDeleteDriver Genius
There
tlinbucktina