/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *l3=new ListNode(-1);#初始化
ListNode *cur=l3;
if(!l1) return l2;
if(!l2) return l1;
while(l1&&l2)
{
if(l1->val>=l2->val)
{
cur->next=l2;
l2=l2->next;
}
else
{
cur->next=l1;
l1=l1->next;
}
cur=cur->next;
}
while(l1)
{
cur->next=l1;
l1=l1->next;
cur=cur->next;
}
while(l2)
{
cur->next=l2;
l2=l2->next;
cur=cur->next;
}
return l3->next;
}
};
剑指offer25合并两个排序的链表
最新推荐文章于 2025-08-06 16:23:50 发布