弱问一个小问题,leetcode 上merge sorted list# JobHunting - 待字闺中
W*e
1 楼
看不太出这个错在哪了,当input 是{},{0} 时候run time error了。
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode *h1=l1,*h2=l2;
ListNode *newHead,*dummy=newHead;
if(l1==NULL&&l2==NULL)
return NULL;
while(h1!=NULL&&h2!=NULL)
{
if(h1->val>=h2->val)
{
dummy->next=h2;
dummy=h2;
h2=h2->next;
}
else
{
dummy->next=h1;
dummy=h1;
h1=h1->next;
}
}
if(h1==NULL)
dummy->next = h2;
else if(h2==NULL)
dummy->next= h1;
return newHead->next;
}
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode *h1=l1,*h2=l2;
ListNode *newHead,*dummy=newHead;
if(l1==NULL&&l2==NULL)
return NULL;
while(h1!=NULL&&h2!=NULL)
{
if(h1->val>=h2->val)
{
dummy->next=h2;
dummy=h2;
h2=h2->next;
}
else
{
dummy->next=h1;
dummy=h1;
h1=h1->next;
}
}
if(h1==NULL)
dummy->next = h2;
else if(h2==NULL)
dummy->next= h1;
return newHead->next;
}