typedef int SListDataType;
//链表中的一个节点
typedef struct Node{
SListDataType value; //值
struct Node *next; //下一个节点的地址
}Node;
//单链表
typedef struct SList{
Node *first;// *head 第一个节点的地址
} SList;
SList* MergeTwoLists(SList *slist1, SList *slist2){
if (slist1 = NULL){
return slist2->first;
}
if (slist2 = NULL){
return slist1->first;
}
Node *c1 = slist1->first;
Node *c2 = slist2->first;
Node *result = NULL;
Node *tail = NULL;
while (c1 != NULL&&c2 != NULL){
if (c1->value <= c2->value){//c1小
if (result == NULL){//头插
result = tail = c1;
}
else{
tail->next = c1;
tail = c1;
}
c1 = c1->next;
}
else{//c2小
if (result == NULL){
result = tail = c2;
}
else{
tail->next = c2;
tail = c2;
}
c2 = c2->next;
}
}//大循环结束
//c1或者c2有一个为空
//两个链表中,有一个链表的节点被取完了
//结果链表,剩余链表
if (c1 != NULL){//把剩余的c1 挂在result的tail->next上就行
tail->next = c1;
}
if (c2 != NULL){//把剩余的c2挂在result的tail->next上就行
tail->next = c2;
}
return result;
}