Why time limit issue is there in this code for merge sorted link lists

#include
using namespace std;
class node{
public:
long data;
node* next;

node(long d){
 data=d;
 next=NULL;  // do not forget this
}

};
void print(node* head){
while(head!=NULL){
cout<data<<" ";
head=head->next;
}
}
void insertAtTail(node*& head,long data){
if(head==NULL){
head= new node(data);
return ;
}
node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=new node(data);
return;
}
node* mergesortedlist(node* a,node* b){
if(a==NULL)
return b;
else if(b==NULL)
return a;
node* c=NULL;
if(a->data<=b->data){
c =a;
c->next=mergesortedlist(a->next,b);
}
else {
c=b;
c->next=mergesortedlist(a,b->next);
}
return c;
}
int main() {
node* head1=NULL;
nodehead2=NULL;
int t;
cin>>t;
while(t–){
long n,m;
long data;
cin>>n;
while(n–)
{
cin>>data;
insertAtTail(head1,data);
}
cin>>m;
while(m–)
{
cin>>data;
insertAtTail(head2,data);
}
node
ans=mergesortedlist(head1,head2);
print(ans);
cout<<endl;
}
return 0;
}

hey @kartikg53, declare those head1 and head2 inside while loop so that a new linked list will be created with every new testcase. Also please share the code saved in Coding Blocks ide as code gets mismatched here

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.