/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!l1&&!l2)
return NULL;
if(!l1)
return l2;
if(!l2)
return l1;
ListNode* head=new ListNode(0);
ListNode* header=head;
while(l1&&l2){
if(l1->val<l2->val){
header->next=l1;
l1=l1->next;
} else {
header->next=l2;
l2=l2->next;
}
header=header->next;
}
if(l1){
header->next=l1;
}
if(l2){
header->next=l2;
}
head=head->next;
return head;
}
};
Leetcode: Merge Two Sorted Lists
最新推荐文章于 2020-07-05 11:22:13 发布