i am storing the address of merged LL in head3 but on displaying data using head its giving merged output
check the code
#include
using namespace std;
class node {
public:
int data;
node *next;
node(int data){
this->data=data;
next=NULL;
}
};
void insert(node *&head,int data){
node *t = new node(data);
node *ptr=head;
if(head==NULL){
head=t;
}
else{
while(ptr->next){
ptr=ptr->next;
}
ptr->next=t;
}
}
void disp(node *ptr){
while(ptr){
cout<data<<" ";
ptr=ptr->next;
}
}
node* merge(node *a ,node *b){
if(a==NULL){
return b;
}
if(b==NULL){
return a;
}
node *c;
if(a->data < b->data){
c=a;
c->next=merge(a->next,b);
}
else{
c=b;
c->next=merge(b->next,a);
}
return c;
}
int main() {
node *head=NULL;
node *head2=NULL;
int n1;
cin>>n1;
for(int i=0;i<n1;i++){
int num;
cin>>num;
insert(head,num);
}
int n2;
cin>>n2;
for(int i=0;i<n2;i++){
int num;
cin>>num;
insert(head2,num);
}
node *head3;
head3 = merge(head,head2);
disp(head);
}