mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-14 05:18:48 +00:00
25 lines
557 B
C++
25 lines
557 B
C++
|
/**
|
||
|
* Definition for singly-linked list.
|
||
|
* struct ListNode {
|
||
|
* int val;
|
||
|
* ListNode *next;
|
||
|
* ListNode(int x) : val(x), next(NULL) {}
|
||
|
* };
|
||
|
*/
|
||
|
class Solution {
|
||
|
public:
|
||
|
ListNode* merge(ListNode* l1, ListNode* l2) {
|
||
|
if (l1 == nullptr) {
|
||
|
return l2;
|
||
|
} else if (l2 == nullptr) {
|
||
|
return l1;
|
||
|
} else if (l1->val < l2->val) {
|
||
|
l1->next = merge(l1->next, l2);
|
||
|
return l1;
|
||
|
} else {
|
||
|
l2->next = merge(l1, l2->next);
|
||
|
return l2;
|
||
|
}
|
||
|
}
|
||
|
};
|