mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-18 00:57:17 +00:00
Initial commit
This commit is contained in:
24
Easy/merge-two-sorted-lists/solution.cpp
Normal file
24
Easy/merge-two-sorted-lists/solution.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode() : val(0), next(nullptr) {}
|
||||
ListNode(int x) : val(x), next(nullptr) {}
|
||||
ListNode(int x, ListNode *next) : val(x), next(next) {}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
ListNode *mergeTwoLists(ListNode *list1, ListNode *list2) {
|
||||
if (list1 == nullptr) {
|
||||
return list2;
|
||||
} else if (list2 == nullptr) {
|
||||
return list1;
|
||||
} else if (list1->val < list2->val) {
|
||||
list1->next = mergeTwoLists(list1->next, list2);
|
||||
return list1;
|
||||
} else {
|
||||
list2->next = mergeTwoLists(list1, list2->next);
|
||||
return list2;
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user