struct student{ int num; struct student *pnext; }; //首先将第二个链表的最后一个节点找到,然后将第一个链表的每一个节点依次跟第二个链表比较,如果第二个 //链表没有,就放到第二个链表的末尾 Stu *mergeTwoList(Stu *head,Stu *head1){ Stu *last=head->pnext; Stu *head1_end=head1->pnext; Stu *last1=head1->pnext; Stu *head_pre=head->pnext;//循环保存last的前一个节点 int flag=0;//初始化假设没有相等的 while (head1_end->pnext!=NULL) { head1_end=head1_end->pnext;//用来保存head1的最后一个节点 } while (last!=NULL) { last1=head1->pnext;//每次外层循环一次,就把里面的复位 while (last1!=NULL) { if (last->num==last1->num) { flag=-1; break; } last1=last1->pnext; } if (flag==0)//没有相同的,加到 { head_pre->pnext=last->pnext; head1_end->pnext=last; head1_end=head1->pnext;//往后一个节点,为了方便后面的添加 last->pnext=NULL;//将最后一个节点指向的下一个节点制为空 } head_pre=last;//记录head链表的前一个节点 last=last->pnext; } return head1; }