Intersection of Two Linked Lists

PROBLEM :

Given two linked lists, your task is to complete the function findIntersection, that returns the intersection of two linked lists.

Input:
The function takes 2 arguments, reference pointer to the head of the first linked list (head1) and reference pointer to the head of the second linked list (head2).
There are multiple test cases and for each test case this function will be called separately.

Output:
The function should return reference pointer to the head of the new list that is formed by the intersection of the two the lists.
Note: The new list formed should be in non-decreasing order.

Constraints:
1<=T<=100
1<=N<=103

Example:
Input:
1
6
9 6 4 2 3 8
5
1 2 8 6 2

Output:
2 6 8

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*
structure of the node is as
struct node
{
int data;
struct node* next;
};
*/

void mergeSort(struct node** ) ;
void splitList(struct node* , struct node** , struct node** ) ;
struct node* mergeList(struct node* , struct node* ) ;
struct node *IntersectionList(struct node* , struct node* ) ;

struct node* findIntersection(struct node* head1, struct node* head2)
{
    if(!head1&&!head2)
        return NULL ;
   
    if(!head1)
        return head2 ;
    if(!head2)
        return head1 ;
       
    mergeSort(&head1) ;
    mergeSort(&head2) ;
   
    return IntersectionList(head1,head2) ;
}

void mergeSort(struct node** headRef)
{
    struct node* head = *headRef;
    struct node* a, *b;
    if ((head == NULL) || (head->next == NULL))
       return ;
    splitList(head, &a, &b);
    mergeSort(&a);
    mergeSort(&b);
    *headRef = mergeList(a, b);
}

void splitList(struct node* source, struct node** frontRef, struct node** backRef)
{
    if(source==NULL) return ;
   
    node *slow,*fast ;
    slow=source ;
    fast=source->next ;
   
    while(fast&&fast->next)
    {
        slow=slow->next ;
        fast=fast->next->next ;
    }
   
    (*frontRef)=source ;
    (*backRef)=slow->next ;
    slow->next=NULL ;
}

struct node* mergeList(struct node* a, struct node* b)
{
    if(!a&&!b)
        return NULL ;
    if(!a)
        return b ;
    if(!b)
        return a ;
       
    node *final ;
    if(a->data<b->data)
    {
        final=a ;
        final->next=mergeList(a->next,b) ;
    }
    else
    {
        final=b ;
        final->next=mergeList(a,b->next) ;
    }
    return final ;
}

struct node *IntersectionList(struct node* a, struct node* b)
{
    if(!a&&!b)
        return NULL ;
    if(!a)
        return NULL ;
    if(!b)
        return NULL ;
   
    node *final ;
    if(a->data<b->data)
    {
        return IntersectionList(a->next,b) ;
    }
    else if(a->data>b->data)
    {
        return IntersectionList(a,b->next) ;
    }
    else
    {
        final=a ;
        final->next=IntersectionList(a->next,b->next) ;
        return final ;
    }
}

--------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )