首页 > 编程语言 > 详细

合并两个递增排序的链表

时间:2015-09-01 21:15:53      阅读:287      评论:0      收藏:0      [点我收藏+]
1 typedef struct Link
2 {
3     int data;
4     struct Link* next;
5 }NODE,*pNODE;

 

 

 1 NODE *merge(pNODE *pphead1, pNODE *pphead2)
 2 {
 3     NODE *mergeHead = NULL, *ptail = NULL;
 4     pNODE pcur1 = *pphead1, pcur2 = *pphead2;
 5     if(pcur1 == NULL)
 6         return pcur2;
 7     else if(pcur2 == NULL)
 8         return pcur1;
 9 
10     if(pcur1->data < pcur2->data)
11     {
12         mergeHead = pcur1;
13         pcur1 = pcur1->next;
14     }
15     else
16     {
17         mergeHead = pcur2;
18         pcur2 = pcur2->next;
19     }
20     ptail = mergeHead;
21     while(pcur1 != NULL && pcur2 != NULL)
22     {
23         if(pcur1->data < pcur2->data)
24         {
25             ptail->next = pcur1;
26             ptail = pcur1;
27             pcur1 = pcur1->next;
28         }
29         else
30         {
31             ptail->next = pcur2;
32             ptail = pcur2;
33             pcur2 = pcur2->next;
34         }
35     }
36     if(pcur1 == NULL && pcur2 == NULL)
37         ptail->next = NULL;
38     if(pcur1 != NULL)
39         ptail->next = pcur1;
40     if(pcur2 != NULL)
41         ptail->next = pcur2;
42 
43     return mergeHead;
44 }

 

合并两个递增排序的链表

原文:http://www.cnblogs.com/cpsmile/p/4776863.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!