-
Notifications
You must be signed in to change notification settings - Fork 0
/
合并K个排序链表 (mergeKLists).cpp
134 lines (120 loc) · 2.53 KB
/
合并K个排序链表 (mergeKLists).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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
**对链表有初始化的操作,以及vector的erase操作,算法思想用到了分治法。
*/
#include <string>
#include <sstream>
#include <algorithm>
#include <stack>
#include <cmath>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 == NULL) {
return l2;
}
if (l2 == NULL) {
return l1;
}
ListNode *rel;
if (l1->val > l2->val) {
rel = l2;
l2 = l1;
l1 = rel;
}
ListNode *A = l1;
ListNode *B = l2;
while (A != NULL && B != NULL) {
if (A->next == NULL && B->val >= A->val) {
l2 = B->next;
B->next = A->next;
A->next = B;
B = l2;
}
else if (/*A->val <= B->val &&*/ B->val <= A->next->val && A->next != NULL) {
l2 = B->next;
B->next = A->next;
A->next = B;
B = l2;
}
else {
A = A->next;
}
}
return l1;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
if (lists.empty()) {
return NULL;
}
if (lists.size() == 1 && lists[0] == NULL) {
return NULL;
}
if (lists.size() == 1 && lists[0] != NULL) {
return lists[0];
}
if (/*lists.size() == 2 &&*/ lists[0] == NULL && lists[1] == NULL) {
return lists[0];
}
if (lists.size() == 2 && lists[0] == NULL && lists[1] != NULL) {
return lists[1];
}
if (lists.size() == 2 && lists[1] == NULL && lists[0] != NULL) {
return lists[0];
}
vector<ListNode*>::iterator it = lists.begin();
for (int i = 0; i < lists.size(); i++) {
if (lists[i] == NULL) {
lists.erase(it + i);
}
}
int n;
int left = 0;
n = lists.size();
while (n > 1) {//每两个进行使用mergeTwoLists进行合并,然后循环直到合并完成
if (n % 2 == 1) {
ListNode *m = new ListNode(-9999);
lists.insert(lists.begin() + n, m);
left++;
}
int k = (n + 1) / 2;
for (int i = 0; i < k; i++) {
lists[i] = mergeTwoLists(lists[i], lists[i + k]);
}
n = k;
}
ListNode* A = lists[0];
while (left != 0) {
A = A->next;
left--;
}
return A;
}
int main() {
ListNode * A1 = new ListNode(1);
ListNode * A2 = new ListNode(4);
ListNode * A3 = new ListNode(5);
ListNode * B1 = new ListNode(1);
ListNode * B2 = new ListNode(3);
ListNode * B3 = new ListNode(4);
ListNode * C1 = new ListNode(2);
ListNode * C2 = new ListNode(6);
A1->next = A2;
A2->next = A3;
B1->next = B2;
B2->next = B3;
C1->next = C2;
vector<ListNode*> lists;
lists.push_back(A1);
lists.push_back(B1);
lists.push_back(C1);
cout<< mergeKLists(lists)->val;
system("pause");
return 0;
}