Wednesday, 21 February 2018

Sorted Merge of Two Linked Lists

Given two sorted linked lists, Merge them so that result is also sorted.
Input Format
N - Elements in first list. 
N space separated integers. 
M - Elements in second list. 
M space separated integers.
Constraints
--
Output Format
Single Line of space separated integers.
Sample Input

1 3 7 

2 4 5 9
Sample Output
1 2 3 4 5 7 9
Explanation
Self explanatory.
Program:
Node *sortedMergeLists(Node *h1, Node *h2)
{
    Node *n;
  if(h1==NULL)
    return h2;
  if(h2==NULL)
    return h1;
    if(h1->data < h2->data)
    {
        n=h1;
      h1->next=sortedMergeLists(h1->next,h2);
    }
    else
    {
        n=h2;
        h2->next=sortedMergeLists(h1,h2->next);
    }
  return n;
}

No comments:

Post a Comment

ASP.NET Core: How to implement Azure Active Directory (AAD) Authentication in ASP.NET Core

  In this tutorial, we will implement security for ASP.NET Core Application using Azure Active Directory (AAD).  In this tutorial, I will co...