-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeKSortedLL.java
More file actions
49 lines (42 loc) · 1.19 KB
/
MergeKSortedLL.java
File metadata and controls
49 lines (42 loc) · 1.19 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
import java.util.PriorityQueue;
import java.util.ArrayList;
import java.util.LinkedList;
class Solution {
ArrayList<LinkedList<Integer>> a;
int K;
Solution(ArrayList<LinkedList<Integer>> arr, int n) {
a = arr;
K = n;
}
public LinkedList<Integer> solution() {
PriorityQueue<Integer> pq = new PriorityQueue<>();
LinkedList<Integer> res = new LinkedList<>();
for (LinkedList<Integer> p : a)
for(Integer i: p)
pq.add(i);
while(!pq.isEmpty())
res.add(pq.remove());
return res;
}
}
public class MergeKSortedLL {
public static void main(String[] args) {
LinkedList<Integer> a = new LinkedList<>();
LinkedList<Integer> b = new LinkedList<>();
LinkedList<Integer> c = new LinkedList<>();
ArrayList<LinkedList<Integer>> arr = new ArrayList<>();
a.add(1);
a.add(10);
a.add(20);
b.add(4);
b.add(11);
b.add(13);
c.add(3);
c.add(8);
c.add(9);
arr.add(a);
arr.add(b);
arr.add(c);
System.out.println(new Solution(arr, 3).solution().toString());
}
}