-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_MergeTwoSLists.cpp
85 lines (75 loc) · 1.83 KB
/
12_MergeTwoSLists.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
/*
ListNode *res, *cur;
res = cur = (ListNode*)malloc(sizeof(ListNode));
res->next = nullptr;
while(l1||l2){
cur->next = (ListNode*)malloc(sizeof(ListNode));
cur->next->next = nullptr;
int x1,x2;
x1 = x2 = INT32_MAX;
if(l1)x1 = l1->val;
if(l2)x2 = l2->val;
cur->next->val = x1<x2?x1:x2;
if(x1 < x2)l1=l1->next;
else l2 = l2->next;
cur = cur->next;
}
return res->next;
*/
if(l1 == nullptr || l2 == nullptr)return l1?l1:l2;
ListNode* res = l1, *last = nullptr;
while(res && l2){
if(res->val <= l2->val){
last = res;
res = res->next;
continue;
}
else{
ListNode* tmp = l2;
l2 = l2->next;
if(last == nullptr){
tmp->next = res;
l1 = tmp;
last = tmp;
}
else {
tmp->next = res;
last = last->next = tmp;
}
}
}
if(l2 != nullptr)last->next = l2;
return l1;
}
};
int main()
{
ListNode N1(1);
ListNode N2(2);
ListNode N3(4);
ListNode N4(1);
ListNode N5(2);
ListNode N6(4);
N1.next = &N2;
N2.next = &N3;
N4.next = &N5;
N5.next = &N6;
Solution s;
ListNode* res = s.mergeTwoLists(&N1,&N4);
while(res){
cout << res->val << ' ';
res = res->next;
}
cout<<endl;
return 0;
}