#include<iostream>
#include<cstdio>
using namespace std;
struct Node
{
int data;
Node * next;
};
void PrintList(Node * head)
{
if(head==NULL)
printf("empty List\n");
Node * p=head;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
cout<<endl;
}
/*
这个递归函数还是比较好想的。 参数是两个链表当前要合并的指针
所以初始时都是头指针,然后定义一个newhead 这个newhead就是两个当前指针中 data小的的指针
并且把这个newhewad返回给之前调用的那个函数的next 这样就连起来了
*/
Node * MergeSortedList(Node * p1,Node *p2)
{
if(p1==NULL) return p2;
if(p2==NULL) return p1;
Node * newhead=NULL;
if(p1->data<p2->data)
{
newhead=p1;
newhead->next=MergeSortedList(p1->next,p2);
}
else
{
newhead=p2;
newhead->next=MergeSortedList(p1,p2->next);
}
return newhead;
}
int main()
{
//freopen("/home/gl/in","r",stdin);
int n;
while(scanf("%d",&n)!=EOF)
{
Node * head1;
if(!n)
head1=NULL;
else
head1=new Node();
Node * p=head1;
for(int i=0;i<n;++i)
{
scanf("%d",&p->data);
if(i==n-1)
{
p->next=NULL;
break;
}
p->next=new Node();
p=p->next;
}
PrintList(head1);
scanf("%d",&n);
Node * head2;
if(!n)
head2=NULL;
else
head2=new Node();
p=head2;
for(int i=0;i<n;++i)
{
scanf("%d",&p->data);
if(i==n-1)
{
p->next=NULL;
break;
}
p->next=new Node();
p=p->next;
}
PrintList(head2);
Node * newhead=MergeSortedList(head1,head2);
PrintList(newhead);
cout<<endl<<endl;
}
return 0;
}