forked from bishweashwarsukla/hactoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeLinkedLists.cpp
37 lines (34 loc) · 876 Bytes
/
MergeLinkedLists.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
Merge two linked list into one linked list
Only the function to solve this problem (not the whole code)
*/
#include <iostream>
using namespace std;
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) {
ListNode* result = new ListNode();
if(list1==NULL){
return list2;
}
if(list2==NULL){
return list1;
}
if(list1->val >= list2->val){
result = list2;
result->next = mergeTwoLists(list1,list2->next);
}
else{
result = list1;
result->next = mergeTwoLists(list1->next,list2);
}
return result;
}
};