Skip to content

Commit cabf5ae

Browse files
refactor 23
1 parent 24f6189 commit cabf5ae

File tree

1 file changed

+18
-17
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+18
-17
lines changed

src/main/java/com/fishercoder/solutions/_23.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,28 @@
1212
* Analyze and describe its complexity.*/
1313

1414
public class _23 {
15-
16-
public ListNode mergeKLists(ListNode[] lists) {
17-
PriorityQueue<ListNode> heap = new PriorityQueue((Comparator<ListNode>) (o1, o2) -> o1.val - o2.val);
18-
19-
for (ListNode node : lists) {
20-
if (node != null) {
21-
heap.offer(node);
15+
public static class Solution1 {
16+
public ListNode mergeKLists(ListNode[] lists) {
17+
PriorityQueue<ListNode> heap = new PriorityQueue((Comparator<ListNode>) (o1, o2) -> o1.val - o2.val);
18+
19+
for (ListNode node : lists) {
20+
if (node != null) {
21+
heap.offer(node);
22+
}
2223
}
23-
}
2424

25-
ListNode pre = new ListNode(-1);
26-
ListNode temp = pre;
27-
while (!heap.isEmpty()) {
28-
ListNode curr = heap.poll();
29-
temp.next = new ListNode(curr.val);
30-
if (curr.next != null) {
31-
heap.offer(curr.next);
25+
ListNode pre = new ListNode(-1);
26+
ListNode temp = pre;
27+
while (!heap.isEmpty()) {
28+
ListNode curr = heap.poll();
29+
temp.next = new ListNode(curr.val);
30+
if (curr.next != null) {
31+
heap.offer(curr.next);
32+
}
33+
temp = temp.next;
3234
}
33-
temp = temp.next;
35+
return pre.next;
3436
}
35-
return pre.next;
3637
}
3738

3839
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy