code:-
#include
using namespace std;
class node{
public:
int data; //for data
node*next;
node(int d){
data=d;
next=NULL;
}
};
//passing pointer by reference
void Insertathead(node*&head,int d){
if(head==NULL){
head=new node(d);
return;
}
node n=new node(d);
n->next=head;
head=n;
}
int length(nodehead ){
int cnt=0;
while(head!=NULL){
cnt++;
head=head->next;
}
return cnt;
}
//take input
node* take_input(){
int d;
cin>>d;
node*head=NULL;
while(d!= -1){
Insertathead(head,d);
cin>>d;
}
return head;
}
void print(nodehead){
while(head!=NULL){
cout<data<<" -> ";
head=head->next;
}
cout<<endl;
}
//operator overloading
ostream& operator<<(ostream& os,nodehead){
print(head);
return os;
}
istream& operator>>(istream& is,node*&head){
head=take_input();
return is;
}
///reverse
node* midpt(nodehead){
if(head->next==NULL or head==NULL){
return head;
}
nodeslow=head;
node*fast=head->next;
while(fast!=NULL and fast->next!=NULL){
fast=fast->next->next;
slow=slow->next;
}
return slow;
}
node* merge(node* a,node* b){
//base case
if(a==NULL){
return b;
}
if(b==NULL){
return a;
}
//recursive
node* c;
if(a->datadata){
c=a;
c->next=merge(a->next,b);
}
else{
c=b;
c->next=merge(a,b->next);
}
return c;
}
int main(){
// nodehead=take_input();
nodehead;
node*head2;
int t;
cin>>t;
for(int i=0;i<t;i++){
int n1,n2;
cin>>n1;
for(int i=0;i<n1;i++){
head=take_input();
}
cin>>n2;
for(int i=0;i<n2;i++){
head2=take_input();
}
}
node* newhead=merge(head,head2);
cout<<newhead<<endl;
}