/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
ListNode* result = new ListNode(-1);
ListNode* ans = result;
while(pHead1 || pHead2)
{
if(!pHead1 && pHead2)
{
ans->next = pHead2;
break;
}
if(pHead1 && !pHead2)
{
ans->next = pHead1;
break;
}
if(pHead1->val < pHead2->val)
{
ans->next = pHead1;
ans = ans->next;
pHead1 = pHead1->next;
}
else
{
ans->next = pHead2;
ans = ans->next;
pHead2 = pHead2->next;
}
}
return result->next;
}
};