-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestMain.cpp
More file actions
176 lines (132 loc) · 3 KB
/
Copy pathtestMain.cpp
File metadata and controls
176 lines (132 loc) · 3 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <vector>
using namespace std;
struct ListNode {
ListNode(int x, char z) : val(x), c(z), next(NULL) {}
ListNode(int x) : val(x), next(NULL) {}
int val;
char c;
ListNode *next;
};
#if 1
class Solution {
public:
void func(){
}
/*遍历每一个队列,找出其中最小的,加入到最新的*/
ListNode* mergeKLists(vector<ListNode*>& lists) {
ListNode newListDummy(0);
ListNode *next = &newListDummy;
ListNode *pSmallstNode;
std::vector<ListNode *>::iterator tmp;
while(next) {
pSmallstNode = NULL;
for (std::vector<ListNode*>::iterator i = lists.begin(); i != lists.end(); ++i) {
if (*i == NULL) continue;
if (!pSmallstNode) pSmallstNode = *i;
if ((*i)->val <= pSmallstNode->val) {
pSmallstNode = *i;
tmp = i;
}
}
if (pSmallstNode) *tmp = (*tmp)->next;
next->next = pSmallstNode;
next = next->next;
}
return newListDummy.next;
}
/*合并两个队列*/
ListNode *mergeTwoList(ListNode *l1, ListNode *l2) {
ListNode newListDummy(0);
ListNode *next = &newListDummy;
while(true) {
if (!l1) {
next->next = l2;
break;
}
if (!l2) {
next->next = l1;
break;
}
}
}
/*合并两个队列*/
ListNode *mergeTwoList2(ListNode *l1, ListNode *l2) {
if (!l1) return l2;
if (!l2) return l1;
ListNode newListDummy(0);
ListNode *next = &newListDummy;
next->next = l1;
ListNode *tmp;
while (l1 && l2) {
if (l1->val < l2->val) {
l1 = l1->next;
} else {
tmp = l2;
l2 = l2->next;
tmp->next = l1;
}
next = next->next;
}
while(next) {
if (!l1) {
next->next = l2;
break;
}
if (!l2) {
next->next = l1;
break;
}
if (l1->val < l2->val) {
next->next = l1;
l1 = l1->next;
} else {
next->next = l2;
l2 = l2->next;
}
next = next->next;
}
}
/*
两个两个的排序,再并入下一个
*/
ListNode *mergeKLists2(std::vector<ListNode *> &lists){
}
};
#endif
int main(int argc, char const *argv[])
{
#if 1
/* code */
struct ListNode a1(1, 'a');
struct ListNode a2(2, 'a');
struct ListNode a3(3, 'a');
struct ListNode a4(4, 'a');
a1.next = &a2;
a2.next = &a3;
a3.next = &a4;
struct ListNode b1(1, 'b');
struct ListNode b2(2, 'b');
struct ListNode b3(3, 'b');
struct ListNode b4(4, 'b');
b1.next = &b2;
b2.next = &b3;
b3.next = &b4;
ListNode *head1 = &a1;
ListNode *head2 = &b1;
ListNode *tmp;
std::vector<ListNode *> v;
v.push_back(head1);
v.push_back(head2);
std::vector<ListNode *>::iterator iter;
iter = v.begin();
cout << "befor size " << v.size();
v.erase(iter);
cout << "end size " << v.size();
// tmp = (*iter);
// (*iter) = (*iter)->next;
// cout << "hello" << v[0]->val << v[0]->type;
// cout << "tmp val = " << tmp->val;
#endif
return 0;
}