class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
{
ListNode *prenode =new ListNode(0);
ListNode *tail=prenode;
while(l1&&l2)
{
if(l1->val<l2->val)
{
tail->next=l1;
l1=l1->next;
tail=tail->next;
}else
{
tail->next=l2;
l2=l2->next;
tail=tail->next;
}
}
if(l1)
tail->next=l1;
if(l2)
tail->next=l2;
return prenode->next;
}
};
21. Merge Two Sorted Lists
最新推荐文章于 2024-02-04 15:23:14 发布