Union of Two Linked Lists

PROBLEM :

Given two linked lists, your task is to complete the function makeUnion, that returns the union 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 union 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:
1 2 3 4 6 8 9

--------------------------------------------------------------------------------
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* ) ;
void RemoveDublicate(node **) ;
struct node *UnionList(struct node* , struct node* ) ;

struct node* makeUnion(struct node* head1, struct node* head2)
{
    if(!head1&&!head2)
        return NULL ;
   
    if(!head1)
        return head2 ;
    if(!head2)
        return head1 ;
       
    mergeSort(&head1) ;
    mergeSort(&head2) ;
   
    RemoveDublicate(&head1) ;
    RemoveDublicate(&head2) ;
   
    return UnionList(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 ;
}

void RemoveDublicate(node **head)
{
    if(!(*head)||!((*head)->next))
        return ;
       
    node *curr,*temp ;
    curr=(*head) ;
   
    while(curr->next)
    {
        if(curr->data==curr->next->data)
        {
            temp=curr->next ;
            curr->next=temp->next ;
            free(temp) ;
        }
        else
            curr=curr->next ;
    }
}

struct node *UnionList(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=UnionList(a->next,b) ;
    }
    else if(a->data>b->data)
    {
        final=b ;
        final->next=UnionList(a,b->next) ;
    }
    else
    {
        final=a ;
        final->next=UnionList(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 )