Merge two linked list

cant find the problem
merge two sorted linked list

#include
using namespace std;

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;//it means that let node*tail starts from head
while(tail->next!=NULL){
	tail=tail->next; //last node of the linked list
}
tail->next=new node(data);//insert the node at the last node
return ;

}

void buildList(node*&head){
int data,n;
cin>>n;

while(n>0){
	cin>>data;
	insertAtTail(head,data);
	n--;
}

}

void print(nodehead){
// node
temp=head;
while(head!=NULL){
cout<data<<" ";
head=head->next;
}
cout<<endl;
}

// istream& operator>>(istream &is,node*&head){
// buildList(head);
// return is;
// }

// ostream& operator<<(ostream &os,node*head){
// print(head);
// return os;
// }

//merge 2 sorted linked lists
node* merge(nodea,nodeb){
if(a==NULL){
return b;
}else if(b==NULL){
return a;
}
node*c;
//compare elemnt a and b
if(a->data < b->data){
c=a;
c->next=merge(a->next,b);//here a->next means that a->next ids the remaining part and b and then again the function will
}else{
c=b;
c->next=merge(a,b->next);//here if c doenot point to a then the smallest element points is in the b node
}
return c;
}

int main(){
int t;
while (t–){
int n;
cin>>n;
node *head = NULL;
node *head2 = NULL;
buildList(head);
cin>>n;
buildList(head2);
merge(head,head2);
print(head);
}
return 0;

}

paste the code on IDE and share the link.

https://ide.codingblocks.com/s/52837

Given 2 sorted linked lists , Merge the two given sorted linked list and print the final LinkedList.

Input Format:
First Line contains T the number of test cases. For each test case : Line 1 : N1 the size of list 1 Line 2 : N1 elements for list 1 Line 3 : N2 the size of list 2 Line 4 : N1 elements for list 2

input format

1
4
1 3 5 7
3
2 4 6

Hey Rakshit, you have made small mistakes in main()

  1. you are not taking test cases input
  2. you are taking input for size of array 2 times once in main() and again in buildList().

Here I have updated you code for main(), you can refer this

    int main(){
    	int t;
    	cin>>t;
    	while (t--){
    		int n;
    		//cin>>n;
    		node *head = NULL;
    		node *head2 = NULL;
    		buildList(head);
    		//cin>>n;
    		buildList(head2);
    		merge(head,head2);
    		print(head);
    	}
    	return 0;

    }

MERGE SORTED LINKED LISTS
Given 2 sorted linked lists , Merge the two given sorted linked list and print the final LinkedList.

Input Format:
First Line contains T the number of test cases. For each test case : Line 1 : N1 the size of list 1 Line 2 : N1 elements for list 1 Line 3 : N2 the size of list 2 Line 4 : N1 elements for list 2

Constraints:
1 <= T <= 1000 0<= N1, N2 <= 10^6 -10^7 <= Ai <= 10^7

Output Format
T lines of merged output

Sample Input
1
4
1 3 5 7
3
2 4 6
Sample Output
1 2 3 4 5 6 7

for this question
when i summit the code it shows wrong answer

my code - https://ide.codingblocks.com/s/52868

Hi Rakshit, you need to print a new line only after every test case. You were printing it multiple times inside print function. Secondly, the return type of your merge function is node* but you were not capturing the return value anywhere in main, thus your code was not working fine. I’ve made some changes in your code and now it’s working correctly. Please go through it here: https://ide.codingblocks.com/s/61363

Hi Rakshit, as you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.