/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) {
// write code here
if(pHead1==NULL)
return pHead2;
if(pHead2==NULL)
return pHead1;
struct ListNode*n1 = pHead1,*n2 = pHead2;
struct ListNode*newNode = NULL,*tail = NULL;
if(n1->val<n2->val)
{
newNode = tail = n1;
n1 = n1->next;
}
else{
newNode = tail = n2;
n2 = n2->next;
}
while(n1&&n2)
{
if(n1->val<n2->val)
{
tail->next = n1;
tail = n1;
n1 = n1->next;
}
else {
tail->next = n2;
tail = n2;
n2 = n2->next;
}
}
if(n1)
tail->next = n1;
if(n2)
tail->next = n2;
return newNode;
}