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.
N space separated integers.
M - Elements in second list.
M space separated integers.
Constraints
--
Output Format
Single Line of space separated integers.
Sample Input
3
1 3 7
4
2 4 5 9
1 3 7
4
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