-
Notifications
You must be signed in to change notification settings - Fork 0
/
21_merge_2sorted.cpp
50 lines (40 loc) · 1.06 KB
/
21_merge_2sorted.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
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <string>
#include <iostream>
#include <sstream>
#include "parse_input.h"
// O(N + M)
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode merge = ListNode();
ListNode* cur = &merge;
while (l1 && l2){
if (l1->val <= l2->val){
cur->next = l1;
l1 = l1->next;
}
else{
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
cur->next = l1? l1 : l2;
return merge.next;
}
};
int main(){
std::string line;
std::cout << "Space separated values of l1: ";
std::getline(std::cin, line);
ListNode* l1 = str2List(line);
line = "";
std::cout << "Space separated values of l2: ";
std::getline(std::cin, line);
ListNode* l2 = str2List(line);
ListNode* l_res = Solution().mergeTwoLists(l1, l2);
std::string answer;
List2str(l_res, answer);
std::cout << "Sorted: " << answer << std::endl;
return 0;
}