//merging 2 sorted linked lists
#include
using namespace std;
struct node
{
int data;
nodenext;
};
void insertEnd(node &head,int val)
{
if(head==NULL)
{
nodeptr=new node;
ptr->data=val;
ptr->next=head;
head=ptr;
}
else
{
nodetemp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
nodeptr=new node();
ptr->data=val;
ptr->next=NULL;
temp->next=ptr;
}
}
void display(node head)
{
while(head!=NULL)
{
cout<data<<" “;
head=head->next;
}
}
node* merge(node* a,node* b)
{
node head;
if(a==NULL)
return b;
else if(b==NULL)
return a;
else if(a->data <= b->data)
{
head=a;
head->next=merge(a->next,b);
}
else
{
head=b;
head->next=merge(a,b->next);
}
return head;
}
int main()
{
int n;
cin>>n;
node ahead=NULL,bhead=NULL;
while(n–)
{
int a,b;
cin>>a;
while(a–)
{
int num;
scanf("%d",&num);
insertEnd(ahead,num);
}
cin>>b;
while(b–)
{
int num;
scanf("%d",&num);
insertEnd(bhead,num);
}
nodec=merge(ahead,bhead);
display©;
printf(”\n");
}
return 0;
}
I am getting a TLE error in this. How do I fix it?
Plz send your code by saving on ide