TLE error in merging two sorted linked list

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;

}

fixed this function here

#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){ Node *c; if(a== NULL && b == NULL){ return NULL; } if(a== NULL){ return a; } if(b==NULL){ return b; } 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; }

i have changed the code but still gettig tle error in cb ide but in other ide it is running perfectly fine but only printing one less element

if u could give me cb ide link to your code, i will be able to help u better, this code is hard to read and understand

please find the link for the code https://ide.codingblocks.com/s/226221

you have simply, pasted the entire code onto one line, this does not help, please provide a proper indented, readable code, thank you

please reload it you will find fresh code neatly written with proper intendation

please use this link https://ide.codingblocks.com/s/226233


here you go, fixed the code
head and d to bre created inside the test cases loop