public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1==NULL) return l2;
if(l2==NULL) return l1;
ListNode* a=new ListNode(1);
ListNode* head=a;
while(l1&&l2)
if(l1->val < l2->val)
{
head->next=l1;
l1=l1->next;
head=head->next;
}
else
{
head->next=l2;
l2=l2->next;
head=head->next;
}
head->next=l1?l1:l2;
return a->next;
}
};
合并两个有序链表(归并)
最新推荐文章于 2024-08-12 22:38:49 发布