struct ListNode
{
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL){}
};
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if(pHead1 == NULL)
return pHead2;
else if(pHead2 == NULL)
return pHead1;
ListNode *pNode1 = pHead1;
ListNode *pNode2 = pHead2;
ListNode *pHead = NULL;
ListNode *pNode = NULL;
if(pNode1 -> val < pNode2 -> val)
{
pHead = pNode1;
pNode1 = pNode1 -> next;
}
else
{
pHead = pNode2;
pNode2 = pNode2 -> next;
}
pNode = pHead;
while(pNode1 && pNode2)
{
if(pNode1 -> val <= pNode2 -> val)
{
pNode -> next = pNode1;
pNode = pNode1;
pNode1 = pNode1 -> next;
}
else
{
pNode -> next = pNode2;
pNode = pNode2;
pNode2 = pNode2 -> next;
}
}
if(pNode1)
pNode -> next = pNode1;
if(pNode2)
pNode -> next = pNode2;
return pHead;
}
struct ListNode
{
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL){}
};
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if(pHead1 == NULL)
return pHead2;
else if(pHead2 == NULL)
return pHead1;
ListNode *pHead = NULL;
if(pHead1 -> val < pHead2 -> val)
{
pHead = pHead1;
pHead -> next = Merge(pHead1 -> next, pHead2);
}
else
{
pHead = pHead2;
pHead -> next = Merge(pHead1, pHead -> next);
}
return pHead;
}