node* MergeList(node*& phead1, node*& phead2)//合并两个有序链表  从小到大

{

//判断链表是否为空

if (phead1)

{

return phead2;   

}

if (phead2)

{

return phead1;

}

//取下链表较小的头节点作为一个新节点头

node* l1_cur = phead1, *l2_cur = phead2;

node* newhead = l1_cur;

if (l1_cur->data > l2_cur->data)

{

newhead = l2_cur;

l2_cur = l2_cur->next;

}

else

l1_cur = l1_cur->next;

node* cur = newhead;

//对两个链表进行比较

while(l1_cur && l2_cur)

{

if (l1_cur->data > l2_cur->data)

{

cur->next = l2_cur->next;

cur = cur->next;

l2_cur = l2_cur->next;

}

else

{

cur->next = l1_cur->next;

cur = cur->next;

l1_cur = l1_cur->next;

}

}

//当一个链表为空 将另一个链表连接到尾部

if (l1_cur)

{

cur->next = l2_cur;

}

if (l2_cur)

{

cur->next = l1_cur;

}

return newhead;

}