Skip to content

Commit

Permalink
Create merge-2-lists.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
gabedonnan authored Mar 29, 2023
1 parent 52d7d30 commit e2d07e9
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions merge-2-lists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for singly-linked list.
* 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 == NULL) {
return list2;
} else if (list2 == NULL) {
return list1;
}

if (list1->val <= list2->val) {
list1->next = mergeTwoLists(list1->next, list2);
return list1;
} else {
list2->next = mergeTwoLists(list1, list2->next);
return list2;
}
}
};

0 comments on commit e2d07e9

Please sign in to comment.