Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4bc8396

Browse files
authored
Update Merge_K_SortedLinkedlist.java
format code
1 parentf3ef5b6 commit4bc8396

File tree

1 file changed

+37
-46
lines changed

1 file changed

+37
-46
lines changed
Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,54 @@
1-
importjava.util.*;
1+
importjava.util.Arrays;
2+
importjava.util.Comparator;
3+
importjava.util.PriorityQueue;
24

5+
/**
6+
* @author Arun Pandey (https://github.com/pandeyarun709)
7+
*/
38
publicclassMerge_K_SortedLinkedlist {
4-
5-
/*
6-
* This function merge K sorted LinkedList
7-
* @param Node []a : array of LinkedList
8-
* @param int N : size of array
9-
*
10-
* author Arun Pandey (https://github.com/pandeyarun709)
11-
*/
12-
NodemergeKList(Node[]a,intN)
13-
{
9+
10+
/**
11+
* This function merge K sorted LinkedList
12+
*
13+
* @param a array of LinkedList
14+
* @param N size of array
15+
* @return node
16+
*/
17+
NodemergeKList(Node[]a,intN) {
1418
// Min Heap
15-
PriorityQueue<Node>min =newPriorityQueue<>(newComparator<Node>(){
16-
17-
publicintcompare(Nodex ,Nodey){
18-
returnx.data -y.data;
19-
}
20-
});
21-
19+
PriorityQueue<Node>min =newPriorityQueue<>(Comparator.comparingInt(x ->x.data));
20+
2221
// adding head of all linkedList in min heap
23-
for(inti =0 ;i<N ;i++)
24-
{
25-
min.add(a[i]);
26-
}
27-
22+
min.addAll(Arrays.asList(a).subList(0,N));
23+
2824
// Make new head among smallest heads in K linkedList
2925
Nodehead =min.poll();
3026
min.add(head.next);
3127
Nodecurr =head;
3228

33-
//merging LinkedList
34-
while(!min.isEmpty()) {
35-
29+
//merging LinkedList
30+
while(!min.isEmpty()) {
31+
3632
Nodetemp =min.poll();
3733
curr.next =temp;
3834
curr =temp;
39-
40-
//Add Node in min Heap only if temp.next is not null
41-
if(temp.next !=null){
35+
36+
//Add Node in min Heap only if temp.next is not null
37+
if(temp.next !=null){
4238
min.add(temp.next);
4339
}
4440
}
45-
46-
returnhead;
47-
48-
49-
}
50-
51-
// Node Class
52-
privateclassNode{
53-
privateintdata;
54-
privateNodenext;
55-
56-
publicNode(intd)
57-
{
58-
this.data =d;
59-
next =null;
60-
}
41+
42+
returnhead;
6143
}
6244

63-
}
45+
privateclassNode {
46+
privateintdata;
47+
privateNodenext;
48+
49+
publicNode(intd) {
50+
this.data =d;
51+
next =null;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp