-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathFebruary-19.cpp
138 lines (119 loc) · 3.1 KB
/
February-19.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
135
136
137
138
//{ Driver Code Starts
// C++ program to merge k sorted arrays of size n each
#include <bits/stdc++.h>
using namespace std;
// A Linked List node
struct Node {
int data;
Node* next;
Node(int x) {
data = x;
next = NULL;
}
};
/* Function to print nodes in a given linked list */
void printList(Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
cout << endl;
}
// } Driver Code Ends
// Custom class for min heap
class Solution {
public:
Node* mergeKLists(vector<Node*>& l) {
auto c=[](Node* a,Node* b){return a->data>b->data;};
priority_queue<Node*,vector<Node*>,decltype(c)> pq(c);
for(auto x:l) if(x) pq.push(x);
Node d(0),*t=&d;
while(!pq.empty()){
t->next=pq.top();t=pq.top();pq.pop();
if(t->next) pq.push(t->next);
}
return d.next;
}
};
2)
class Solution {
public:
Node* mergeKLists(vector<Node*>& lists) {
if (lists.empty()) return nullptr;
while (lists.size() > 1) {
vector<Node*> temp;
for (int i = 0; i < lists.size(); i += 2)
temp.push_back(merge(lists[i], i + 1 < lists.size() ? lists[i + 1] : nullptr));
lists = temp;
}
return lists[0];
}
Node* merge(Node* a, Node* b) {
if (!a || !b) return a ? a : b;
if (a->data > b->data) swap(a, b);
a->next = merge(a->next, b);
return a;
}
};
3)
class Solution {
public:
Node* mergeKLists(vector<Node*>& lists) {
vector<int> v;
for (auto l : lists) while (l) v.push_back(l->data), l = l->next;
sort(v.begin(), v.end());
Node d(0), *t = &d;
for (int x : v) t->next = new Node(x), t = t->next;
return d.next;
}
};
4)
class Solution {
public:
Node* merge(Node* a, Node* b) {
if (!a) return b;
if (!b) return a;
if (a->data < b->data) { a->next = merge(a->next, b); return a; }
b->next = merge(a, b->next); return b;
}
Node* mergeKLists(vector<Node*>& lists) {
if (lists.empty()) return nullptr;
while (lists.size() > 1) {
lists.push_back(merge(lists[0], lists[1]));
lists.erase(lists.begin()), lists.erase(lists.begin());
}
return lists.front();
}
};
//{ Driver Code Starts.
// Driver program to test the above functions
int main() {
int t;
cin >> t;
cin.ignore();
while (t--) {
int n;
cin >> n;
cin.ignore();
vector<Node*> v(n);
for (int i = 0; i < n; i++) {
string line;
getline(cin, line);
stringstream ss(line);
Node* head = new Node(0);
Node* temp = head;
int x;
while (ss >> x) {
Node* newNode = new Node(x);
temp->next = newNode;
temp = temp->next;
}
v[i] = head->next;
}
Solution ob;
Node* head = ob.mergeKLists(v);
printList(head);
}
return 0;
}
// } Driver Code Ends