why this code is showing tle error what is other way to solve it if so ??
#include
using namespace std;
class Node{
public:
int data;
Node *next;
};
void insertAtLast(Node *&head,int data){
Node *n = new Node;
Node *last =head;
n->data= data;
n->next =NULL;
if(head == NULL){
head = n;
return;
}
while(last ->next != NULL){
last = last->next;
}
last->next =n;
}
Node * merge(Node *a,Node *b){
if(a== NULL){
return a;
}
if(b==NULL){
return b;
}
Node *c;
if(a->data < b->data){
c=a;
c->next = merge(a->next,b);
}
else{
c=b;
c->next=merge(a,b->next);
}
return c;
}
void printList(Node *head){
while(head !=NULL){
cout<data<<" ";
head =head->next;
}
}
int main() {
Node *head = NULL;
Node *head1 = NULL;
int t;
cin>>t;
while(t>0){
int n1;
cin>>n1;
while(n1>0){
int data;
cin>>data;
insertAtLast(head,data);
n1--;
}
int n2;
cin>>n2;
while(n2>0){
int data;
cin>>data;
insertAtLast(head1,data);
n2--;
}
Node *d= merge(head,head1);
printList(d);
t--;
}
return 0;
}