Merge two sorted linked lists such that merged list is in reverse order O(n)

Merge two sorted linked lists such that merged list is in reverse order



Basically we have to merge two linked list in reverse order which are already sorted. for ex if we have two linked list L1=5,6,7 and L2=2,3,4 then the output should be 7,6,5,4,3,2 .

What we do is first compare the both linked list from starting and put the greater element in the beginning of the resultant linked list.
If the first list or second list become null then pull the elements in the resultant list.

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    int next;
}*head1,*head2,*result;



void insert1(int num)
{
    struct node* temp=(struct node*)malloc(sizeof(struct node));
    temp->data=num;
    temp->next=NULL;
    if(head1==NULL)
    {
        head1=temp;
    }
    else
    {
        struct node* temp1=head1;
        while(temp1->next!=NULL)
        {
            temp1=temp1->next;
        }
        temp1->next=temp;
    }
}

void insert2(int num)
{
    struct node* temp=(struct node*)malloc(sizeof(struct node));
    temp->data=num;
    temp->next=NULL;
    if(head2==NULL)
    {
        head2=temp;
    }
    else
    {
        struct node* temp1=head2;
        while(temp1->next!=NULL)
        {
            temp1=temp1->next;
        }
        temp1->next=temp;
    }
}

void display(struct node* head)
{
    if(head==NULL)
    {
        printf("List is Empty");

    }
    else
    {
        while(head!=NULL)
        {
        printf("%d",head->data);
        printf("-->");
        head=head->next;
        }


    }

}
void insert_in_result(int num)
{
    struct node* temp=(struct node*)malloc(sizeof(struct node));
    temp->data=num;
    if(result==NULL)
    {

        temp->next=NULL;
        result=temp;
    }
    else
    {
        struct node* temp1=result;
        temp->next=temp1;
        result=temp;

    }
}



void merged_list_in_the_reverse_order(struct node* head1, struct node* head2)
{
    while(head1!=NULL && head2!=NULL)
    {
        if(head1->data<head2->data)
        {
            insert_in_result(head1->data);
            head1=head1->next;

        }
        else
        {
            insert_in_result(head2->data);
            head2=head2->next;
        }
    }

    while(head1!=NULL)
    {
        insert_in_result(head1->data);
        head1=head1->next;
    }
        while(head2!=NULL)
    {
        insert_in_result(head2->data);
        head2=head2->next;
    }

   display(result);


}






int  main()
{
    int i,num;
    int pos;
    struct node *n;
    head1=NULL;
    head2=NULL;
    result=NULL;
    while(1)
    {
    printf("\nList Operations\n");
    printf("===============\n");
    printf("1.Insert the number in the first sorted list\n");
    printf("2. Insert the number in the second sorted list\n");
    printf("3.Display the first linked list\n");
    printf("4. Display the second linked list\n");
    printf("5.Merge two sorted list in the reverse order\n");
    printf("6.Exit\n");

    printf("Enter your choice : ");
    if(scanf("%d",&i)<=0){
        printf("Enter only an Integer\n");
        exit(0);
    } else {
        switch(i)
        {
        case 1:
            printf("Enter the number to insert in the first linked list: \n");
            scanf("%d",&num);
            insert1(num);
            break;
        case 2:
            printf("Enter the number to insert in the second linked list \n: ");
            scanf("%d",&num);
            insert2(num);
            break;

        case 3:
            if(head1==NULL)
                {
                printf("List is Empty\n");
                }
                else
                {
                printf("Element(s) in the list are : ");
                }
                display(head1);
                break;
        case 4:
             if(head2==NULL)
                {
                printf("List is Empty\n");
                }
                else
                {
                printf("Element(s) in the list are : ");
                }
                display(head2);
                break;
        case 5:
            if(head1==NULL && head2==NULL)
            {
                printf("List is Empty\n");
            }
            else
            {
                printf("Printing the merged list with reverse sorted order\n");
            }
            merged_list_in_the_reverse_order(head1,head2);
            break;
         case 6:
             return 0;
        }
    }
    }

    return 0;
}

No comments:

Post a Comment

All Repeated Words In Text File And Their Occurrences In Java

Find All Repeated Words In Text File And Their Occurrences In Java Place your file location in the argument of the FileReader. If t...