标签:
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
ListNode* res = new ListNode(0);
if (l1 == NULL)
{
return l2;
}
else if (l2 == NULL)
{
return l1;
}
ListNode* head = res;
int l1val = 0, l2val = 0;
while (l1 != NULL&& l2!=NULL)
{
if (l1->val < l2->val)
{
head->next = l1;
l1 = l1->next;
head = head->next;
}
else
{
head->next = l2;
l2 = l2->next;
head = head->next;
}
}
if (l1)
{
head->next = l1;
}
else
{
head->next = l2;
}
return res->next;
}
};
标签:
原文地址:http://www.cnblogs.com/flyjameschen/p/4381853.html