-
题目描述
-
解题思路心得
和225差不多,只是先用头插法建a,b变递增,然后一人一个指针比较,要大的,允许重复就不用考虑一样的情况
-
代码
#include <iostream>
using namespace std;
struct LNode
{
int data;
LNode *next;
};
// 头插法
void createlist(LNode *&L, int length)
{
LNode *p = L;
for (int i = 0; i < length; i++)
{
LNode *tmp = new LNode;
cin >> tmp->data;
tmp->next = p->next;
p->next = tmp;
}
}
void Printlist(LNode *L)
{
LNode *p = L->next;
while (p)
{
cout << p->data;
if (p->next != NULL)
{
cout << " ";
}
p = p->next;
}
cout << endl;
}
LNode* sortlist(LNode *&LA, LNode *&LB)
{
LNode *pa = LA->next;
LNode *pb = LB->next;
LNode *c = new LNode;
c->next = NULL;
LNode *pc = c;
while (pa!=NULL||pb!=NULL)
{
if (pa == NULL)
{
pc->next =pb;
break;
}
if (pb == NULL)
{
pc->next =pa;
break;
}
if (pa->data > pb->data)
{
pc->next=pa;
pa = pa->next;
pc =pc->next;
pc->next=NULL;
}
else
{
pc->next=pb;
pb = pb->next;
pc =pc->next;
pc->next=NULL;
}
}
return c;
}
int main()
{
int anum = 0, bnum = 0;
while (1)
{
LNode *a = new LNode;
LNode *b = new LNode;
a->next = NULL;
b->next = NULL;
cin >> anum >> bnum;
if (anum == 0 || bnum == 0)
{
break;
}
createlist(a, anum);
createlist(b, bnum);
Printlist(sortlist(a, b));
}
return 0;
}