0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:31:59 +00:00

36. 合并两个排序的链表

https://www.acwing.com/problem/content/submission/code_detail/14064359/
This commit is contained in:
Baoshuo Ren 2022-05-09 19:33:30 +08:00
parent 0fbecbac25
commit e0b77729a1
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

24
AcWing/36/36.cpp Normal file
View File

@ -0,0 +1,24 @@
/**
* 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;
}
}
};