-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path23.cpp
More file actions
executable file
·62 lines (60 loc) · 1.72 KB
/
23.cpp
File metadata and controls
executable file
·62 lines (60 loc) · 1.72 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
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
bool check(vector<ListNode*>& lists){
for (int i = 0; i < lists.size();i++)
if (lists[i] != NULL)
return true;
return false;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
if (lists.empty())
return NULL;
vector<ListNode*> current = lists;
ListNode* answer = new ListNode(0);
ListNode* head = answer;
while (check(current)){
int mini = 1000;
int index = 0;
for (int i = 0; i <lists.size(); i++)
if (current[i] != NULL && current[i]->val < mini){
mini = current[i]->val;
index = i;
}
ListNode * tmp = new ListNode(current[index]->val);
//printf("%d\n",current[index]->val);
answer->next = tmp;
answer = answer ->next;
current[index] = current[index] -> next;
}
return head -> next;
}
};
int main(){
Solution s;
vector<ListNode*> current;
ListNode* p1 = new ListNode(1);
ListNode* p1_2 = new ListNode(4);
ListNode* p1_3 = new ListNode(4);
p1_2->next = p1_3;
p1->next = p1_2;
ListNode* p2 = new ListNode(1);
ListNode* p2_2 = new ListNode(3);
ListNode* p2_3 = new ListNode(4);
ListNode* p3 = new ListNode(2);
ListNode* p3_2 = new ListNode(6);
p3->next = p3_2;
current.push_back(p1);
current.push_back(p2);
current.push_back(p3);
s.mergeKLists(current);
}