Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aa2900e

Browse files
refactor 23
1 parent f9f6732 commit aa2900e

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ Your ideas/fixes/algorithms are more than welcome!
579579
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_26.java)|O(n)|O(1)|Easy| Array
580580
|25|[Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_25.java)|O(n)|O(1)| Hard | Recursion, LinkedList
581581
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_24.java)|O(n)|O(h)|Medium| Recursion, LinkedList
582-
|23|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_23.java)|O(n*logk)|O(logk)|Hard|Heap
582+
|23|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_23.java)|O(n*logk)|O(k)|Hard|Heap
583583
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_22.java)|TBD|O(n)|Medium|Backtracking
584584
|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_21.java)|O(n)|O(h)|Easy| Recursion
585585
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_20.java)|O(n)|O(n)|Easy|Stack

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,37 @@
55
import java.util.Comparator;
66
import java.util.PriorityQueue;
77

8-
/**Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.*/
8+
/**
9+
* 23. Merge k Sorted Lists
10+
*
11+
* Merge k sorted linked lists and return it as one sorted list.
12+
* Analyze and describe its complexity.*/
913

1014
public class _23 {
15+
1116
public ListNode mergeKLists(ListNode[] lists) {
12-
PriorityQueue<ListNode> heap = new PriorityQueue(new Comparator<ListNode>(){
17+
PriorityQueue<ListNode> heap = new PriorityQueue(new Comparator<ListNode>() {
1318
@Override
1419
public int compare(ListNode o1, ListNode o2) {
1520
return o1.val - o2.val;
1621
}
1722
});
18-
19-
for(ListNode node : lists){
20-
if(node != null) heap.offer(node);
23+
24+
for (ListNode node : lists) {
25+
if (node != null) heap.offer(node);
2126
}
22-
27+
2328
ListNode pre = new ListNode(-1);
2429
ListNode temp = pre;
25-
while(!heap.isEmpty()){
30+
while (!heap.isEmpty()) {
2631
ListNode curr = heap.poll();
27-
temp.next = curr;
32+
temp.next = new ListNode(curr.val);
33+
if (curr.next != null) {
34+
heap.offer(curr.next);
35+
}
2836
temp = temp.next;
29-
if(curr.next != null) heap.offer(curr.next);
3037
}
3138
return pre.next;
3239
}
40+
3341
}

0 commit comments

Comments
 (0)