Intersection of two sorted Linked lists
PROBLEM :
Given two lists sorted in increasing order, create a new list representing the intersection of the two lists. The new list should be made with its own memory — the original lists should not be changed.
For example, let the first linked list be 1->2->3->4->6 and second linked list be 2->4->6->8, then your function should create a third list as 2->4->6.
Input:
You have to complete the method which takes 3 argument: the head of the first linked list , the head of second linked list and the head of the third link list which is to be created. 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:
Complete the Function given to produce the desired list with intersectioned values.
Constraints:
1 <=T<= 20
1 <= size of linked lists <= 30
1 <= Data in linked list nodes <= 30
Example:(The input/output format below should be used for Expected Output only)
Input
1 --> (No of test cases)
5 4 --> (sizes of linked lists)
1 2 3 4 6 --> (Elements of 1st linked list)
2 4 6 8 --> (Elements of 2nd linked list)
Output
2 4 6 --> (Elements of resultant 3rd linked list
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* The structure of the Linked list Node is as follows:
struct node {
int val;
struct node* next;
}; */
void intersection(struct node **head1, struct node **head2, struct node **head3)
{
struct node *one,*two,*temp,*ptr ;
one=(*head1) ;
two=(*head2) ;
while(one!=NULL&&two!=NULL)
{
temp=(struct node*)malloc(sizeof(struct node)) ;
temp->next=NULL ;
if(one->val>two->val)
two=two->next ;
else if(one->val<two->val)
one=one->next ;
else
{
temp->val=one->val ;
one=one->next ;
two=two->next ;
if((*head3)==NULL)
(*head3)=temp ;
else
{
ptr=(*head3) ;
while(ptr->next!=NULL)
ptr=ptr->next ;
ptr->next=temp ;
}
}
}
}
---------------------------------------------------------------------------------
Given two lists sorted in increasing order, create a new list representing the intersection of the two lists. The new list should be made with its own memory — the original lists should not be changed.
For example, let the first linked list be 1->2->3->4->6 and second linked list be 2->4->6->8, then your function should create a third list as 2->4->6.
Input:
You have to complete the method which takes 3 argument: the head of the first linked list , the head of second linked list and the head of the third link list which is to be created. 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:
Complete the Function given to produce the desired list with intersectioned values.
Constraints:
1 <=T<= 20
1 <= size of linked lists <= 30
1 <= Data in linked list nodes <= 30
Example:(The input/output format below should be used for Expected Output only)
Input
1 --> (No of test cases)
5 4 --> (sizes of linked lists)
1 2 3 4 6 --> (Elements of 1st linked list)
2 4 6 8 --> (Elements of 2nd linked list)
Output
2 4 6 --> (Elements of resultant 3rd linked list
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* The structure of the Linked list Node is as follows:
struct node {
int val;
struct node* next;
}; */
void intersection(struct node **head1, struct node **head2, struct node **head3)
{
struct node *one,*two,*temp,*ptr ;
one=(*head1) ;
two=(*head2) ;
while(one!=NULL&&two!=NULL)
{
temp=(struct node*)malloc(sizeof(struct node)) ;
temp->next=NULL ;
if(one->val>two->val)
two=two->next ;
else if(one->val<two->val)
one=one->next ;
else
{
temp->val=one->val ;
one=one->next ;
two=two->next ;
if((*head3)==NULL)
(*head3)=temp ;
else
{
ptr=(*head3) ;
while(ptr->next!=NULL)
ptr=ptr->next ;
ptr->next=temp ;
}
}
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment