版权声明:本博文欢迎分享与转载,转载请注明出处和作者。 https://blog.csdn.net/dream6104/article/details/88830293
/**
* 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) {
if(l1==NULL) return l2;
if(l2==NULL) return l1;
ListNode *h2=new ListNode(0);
ListNode *h=h2;
while(l1!=NULL&&l2!=NULL){
if(l1->val<=l2->val) {
h->next=l1;
l1=l1->next;
}
else{
h->next=l2;
l2=l2->next;
}
h=h->next;
}
if(l2!=NULL){h->next=l2;}
if(l1!=NULL){h->next=l1;}
return h2->next;
}
};