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

Commita8edbc1

Browse files
HARD/src/hard/MergeKSortedList.java
1 parentd882581 commita8edbc1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎HARD/src/hard/MergeKSortedList.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
packagehard;
2+
3+
importjava.util.Comparator;
4+
importjava.util.PriorityQueue;
5+
6+
importclasses.ListNode;
7+
8+
/**As discussed with 司德君,王昊,薛博, this is super easy as long as you can think of heap, offer each of the head node into the min-heap.*/
9+
10+
publicclassMergeKSortedList {
11+
publicListNodemergeKLists(ListNode[]lists) {
12+
PriorityQueue<ListNode>heap =newPriorityQueue(newComparator<ListNode>(){
13+
@Override
14+
publicintcompare(ListNodeo1,ListNodeo2) {
15+
returno1.val -o2.val;
16+
}
17+
});
18+
19+
for(ListNodenode :lists){
20+
if(node !=null)heap.offer(node);
21+
}
22+
23+
ListNodepre =newListNode(-1);
24+
ListNodetemp =pre;
25+
while(!heap.isEmpty()){
26+
ListNodecurr =heap.poll();
27+
temp.next =curr;
28+
temp =temp.next;
29+
if(curr.next !=null)heap.offer(curr.next);
30+
}
31+
returnpre.next;
32+
}
33+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp