cpp
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LinkNode {
ElemType data;
LinkNode* next;
}LinkNode,*LinkList;
void creatLinkList(LinkList& L, int m) {
L = (LinkNode*)malloc(sizeof(LinkNode));
L->next = NULL;
int num;
for (int i = 0; i < m; i++) {
scanf("%d", &num);
LinkNode* pnode = (LinkNode*)malloc(sizeof(LinkNode));
pnode->data = num;
pnode->next = L->next;
L->next = pnode;
}
}
void printLinkList(LinkList L) {
LinkNode* pcur = L->next;
while (pcur != NULL) {
printf("%d ", pcur->data);
pcur = pcur->next;
}
}
void insert(LinkNode*& ptail, LinkNode* pnode) {
if (ptail->data == pnode->data) {
free(pnode);
}
else {
pnode->next = ptail->next;
ptail->next = pnode;
ptail = pnode;
}
}
LinkList mergeTwoList(LinkList& La, LinkList& Lb) {
LinkNode* pcurA = La->next;
LinkNode* pcurB = Lb->next;
LinkList Lc = La;
Lc->next = NULL;
LinkNode* ptail = Lc;
free(Lb);
while (pcurA != NULL && pcurB != NULL) {
if (pcurA->data < pcurB->data) {
LinkNode* pnode = pcurA;
pcurA = pcurA->next;
insert(ptail, pnode);
}
else {
LinkNode* pnode = pcurB;
pcurB = pcurB->next;
insert(ptail, pnode);
}
}
if (pcurA != NULL) {
ptail->next = pcurA;
}
if (pcurB != NULL) {
ptail->next = pcurB;
}
return Lc;
}
int main() {
LinkList La, Lb;
int m, n;
scanf("%d", &m);
creatLinkList(La, m);
scanf("%d", &n);
creatLinkList(Lb, n);
LinkList Lc=mergeTwoList(La, Lb);
/*printLinkList(La);
printf("\n");
printLinkList(Lb);*/
printLinkList(Lc);
return 0;
}