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;
}