TLE occurring even when the code matches with the solution

code:

class node{

public:

int data;

node*next;

node(int d){

    data = d;

    next = NULL;

}

};

void insertAtTail(node*&head,int data){

if(head==NULL){

	head = new node(data);

	return;

}

node*tail = head;

while(tail->next!=NULL){

	tail = tail->next;

}

tail->next = new node(data);

return;

}

node* merge(nodea,nodeb){

if(a==NULL){

    return b;

}

else 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(a,b->next);

}

return c;

}

void print(node*head){

while(head!=NULL){

	cout<<head->data<<" ";

	head = head->next;

}

cout<<endl;

}

int main(){

node*a = NULL;

node*b = NULL;

node*c = NULL;

int t;

cin>>t;

   while(t--){

        int n1,n2;

        cin>>n1;

    while(n1--){

            int num;

            cin>>num;
   
            insertAtTail(a,num);
   
        }
    
        cin>>n2;
    
        while(n2--){

      	    int num;

           	    cin>>num;
	
                insertAtTail(b,num);
    
       }
   
        c = merge(a,b);
    
        print(c);
    
        cout<<endl;
 
    }

return 0;
}

@ANIKET_DALAL please save your code on ide.codingblocks.com and share the link.

hi @ANIKET_DALAL declare a, b, and c inside the while loop so that they get re-initialised to 0 for each test case.

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.