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