List Merge(List L1, List L2)
{
if (L1 == NULL)
return L2;
if (L2 == NULL)
return L1;
// 定义指针p和q分别指向链表L1和L2的头结点
List p = L1->Next;
List q = L2->Next;
// 定义新链表的头结点和指针r
List head = (List)malloc(sizeof(struct Node));
head->Next = NULL;
List r = head;
// 比较p和q指向的结点的数据大小,将较小的结点插入到新链表中,并更新对应的指针
while (p != NULL && q != NULL)
{
if (p->Data <= q->Data)
{
r->Next = p;
p = p->Next;
}
else
{
r->Next = q;
q = q->Next;
}
r = r->Next;
}
// 将剩余部分直接连接到新链表的后面
if (p != NULL)
r->Next = p;
if (q != NULL)
r->Next = q;
L1->Next = NULL;
L2->Next = NULL;
return head;
}