两个有序链表,合并成一个有序链表
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
using namespace std;
typedef struct Lnode
{
int data;
struct Lnode *next;
}L,*link;
void creat(link &h,int n)
{
h=new L;
h->next=NULL;link r=h;
while(n--)
{
link p;
p=new L;
cin>>p->data;
p->next=NULL;
r->next=p;
r=p;
}
}
void Union(link la,link lb,link &p)
{
link a,b;
a=la->next;b=lb->next;
p=new L;
p->next=NULL;
link lc=p;
while(a!=NULL&&b!=NULL)
{
if(a->data>b->data)
{
lc->next=b;
lc=b;
b=b->next;
}
else
{
lc->next=a;
lc=a;
a=a->next;
}
}
if(a!=NULL)
{
lc->next=a;
}
if(b!=NULL)
{
lc->next=b;
}
free(a);
free(b);
}
int main()
{
//freopen("in.txt","r",stdin);
link la,lb,lc;
int n,m;
cin>>n>>m;
creat(la,n);
creat(lb,m);
Union(la,lb,lc);
link p=lc->next;
while(p!=NULL)
{
cout<<p->data<<endl;
p=p->next;
}
return 0;
}