/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
typedef struct ListNode listnode;
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2) {
if(pHead1==NULL || pHead2 ==NULL )
{
return (pHead1==NULL)? pHead2 :pHead1;//三目操作符
}
listnode*n1,*n2;
n1 = pHead1;
n2 = pHead2;
listnode *phead,*ptail;
phead = ptail = (listnode*)malloc(sizeof(listnode));//定义哨兵结点
while(n1 && n2)
{
if(n1->val < n2->val)
{
ptail->next = n1;
ptail = ptail->next;
n1 = n1->next;
}
else
{
ptail->next = n2;
ptail = ptail->next;
n2 = n2->next;
}
}
if(n1)
{
ptail->next = n1;
}
if(n2)
{
ptail->next = n2;
}
listnode*pf = phead->next;
free(phead);//销毁堆上申请的空间
phead = ptail = NULL;
return pf;
}