1、另开链表的做法
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* newList=new ListNode;
ListNode* cru=newList;
while(l1!=NULL||l2!=NULL)
{
if(l1!=NULL&&l2!=NULL)
{
if(l1->val<=l2->val)
{
cru->next=new ListNode(l1->val);
cru=cru->next;
l1=l1->next;
}
else
{
cru->next=new ListNode(l2->val);
cru=cru->next;
l2=l2->next;
}
}
else if(l1!=NULL)
{
cru->next=l1;
l1=NULL;
}
else if(l2!=NULL)
{
cru->next=l2;
l2=NULL;
}
}
return newList->next;
}
};