merge two sorted linked lists
// Complete soultion by me
//I will request to try this on your own first
#include<iostream>
using namespace std;
struct node
{
int data;
node* next;
};
node* head=NULL;
node * head1=NULL;
node* headbed=NULL;
void insertion(node **head2 ,int key)
{
node * temp=new node;
temp->data=key;
temp->next=NULL;
if(*head2==NULL)
{
*head2=temp;
}
else
{
node * ptr=*head2;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
void merge()
{
node *ptr=head;
node * str=head1;
while(ptr!=NULL&&str!=NULL)
{
if((ptr->data)<(str->data))
{
insertion(&headbed,ptr->data);
ptr=ptr->next;
}
else
{
insertion(&headbed,(str->data));
str=str->next;
}
}
while(ptr!=NULL)
{
insertion(&headbed,ptr->data);
ptr=ptr->next;
}
while(str!=NULL)
{
insertion(&headbed,str->data);
str=str->next;
}
}
void print(node *heads)
{
node* temp=heads;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"NULL";
}
int main()
{
int n;
cin>>n;
int value;
for(int i=0;i<n;i++)
{
cin>>value;
insertion(&head,value);
}
int n2;
cin>>n2;
for(int i=0;i<n2;i++)
{
cin>>value;
insertion(&head1,value);
}
merge();
print(headbed);
return 0;
}