将两个按值排序的带头结点的单链表La和Lb排列成一个升序的
单链表,并返回一个新的单链表的表头指针
(两个升序合并成升序,用尾插法)
LinkList Merge_LinkList(LNode* La, LNode* Lb)
{//准备工作LNode* Lc;//新链表的头结点LNode* pc;//新链表的工作指针LNode* pa;//La的工作指针LNode* pb;//Lb的工作指针LNode* ptr;//用于删除结点时保存指针Lc = La;//Lc用La的头结点pc = La;//pc用于尾指针pa = La->next;//工作指针初始化pb = Lb->next;//工作指针初始化//合并while (pa != NULL && pb != NULL){if (pa->data < pb->data){pc->next = pa;pc = pa;//pa尾插pa = pa->next;//pa后移}else if (pa->data > pb->data){pc->next = pb;pc = pb;//pb尾插pb = pb->next;//pb后移}else//相等{pc->next = pa;pc = pa;//二者插一个即可pa = pa->next;//后移ptr = pb;pb = pb->next;//将另一个删除free(ptr);}}if (pa != NULL)//pa不空pc->next = pa;//将pa接到pc的后继结点if (pb != NULL)pc->next = pb;free(Lb);//释放Lb链表return Lc;
}