/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param l1 ListNode类
* @param l2 ListNode类
* @return ListNode类
*/
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
// write code here
ListNode* l3=new ListNode(0);
ListNode* p=l1;
ListNode* q=l2;
ListNode* r=l3;
while(p&&q){
if(p->val<=q->val){
r->next=p;
p=p->next;
r=r->next;
}
else{
r->next=q;
q=q->next;
r=r->next;
}
}
if(p!=nullptr)
r->next=p;
if(q!=nullptr)
r->next=q;
return l3->next;
}
};
ListNode* l3=new ListNode(0);创建头节点