/**

  • 合并两个链表

  • @param La 链表La

  • @param Lb 链表Lb

  • @return 合并后的链表 */ LinkList CombineList(LinkList La, LinkList Lb) { LinkList lc, head, h1, h2; // 创建头节点 head = (LinkList)malloc(sizeof(LNode)); head->next = NULL; lc = head; // 遍历La链表 La = La->next; h1 = La; // 遍历Lb链表 h2 = Lb; Lb = Lb->next; lc->next = NULL; int lena = 0, lenb = 0; // 计算La链表的长度 while (h1) { h1 = h1->next; lena++; } // 计算Lb链表的长度 while (h2) { h2 = h2->next; lenb++; }

    // 合并La和Lb链表 while (La || Lb) { if (lena < lenb) { if (La) { lc->next = La; lc = La; La = La->next; } if (Lb) { lc->next = Lb; lc = Lb; Lb = Lb->next; } } else { if (Lb) { lc->next = Lb; lc = Lb; Lb = Lb->next; } if (La) { lc->next = La; lc = La; La = La->next; } } } lc->next = NULL; return head; }

写注释LinkList CombineListLinkList LaLinkList Lb LinkList lcheadh1h2; head = LinkListmallocsizeofLNode; head-next =NULL; lc =head; La = La-next; h1 = La; h2 = Lb; Lb = Lb-next;

原文地址: https://www.cveoy.top/t/topic/jbIC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录