-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeTwoSortedLists.cc
73 lines (55 loc) · 1.17 KB
/
MergeTwoSortedLists.cc
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <vector>
#include <string>
#include <iostream>
using std::vector;
using std::string;
using std::cout;
using std::endl;
#include <cstdlib>
using std::atoi;
struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x), next(NULL){}
};
#include <algorithm>
#include <cstring>
class Solution{
public:
ListNode* mergeTwoLists(ListNode *l1, ListNode * l2){
ListNode h(0);
auto head = &h;
head->next = l1;
auto pre = head;
auto p1 = l1, p2 = l2;
while(p1 && p2){
if(p1->val <= p2->val){
pre = p1;
p1 = p1->next;
}else{
auto next = p2->next;
pre->next = p2;
p2->next = p1;
pre = p2;
p2 = next;
}
}
if(p2){
pre->next = p2;
}
return head->next;
}
};
int main(){
Solution slu;
using std::cin;
auto l1 = new ListNode(2);
auto l2 = new ListNode(1);
auto ans = slu.mergeTwoLists(l1, l2);
while(ans){
cout << ans->val << " ";
ans = ans->next;
}
cout << endl;
return 0;
}