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