-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-23-Merge-k-Sorted-Lists.java
More file actions
131 lines (105 loc) · 3.93 KB
/
LeetCode-23-Merge-k-Sorted-Lists.java
File metadata and controls
131 lines (105 loc) · 3.93 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
LeetCode: https://leetcode.com/problems/merge-k-sorted-lists/
LintCode: http://www.lintcode.com/problem/merge-k-sorted-lists/
JiuZhang: http://www.jiuzhang.com/solutions/merge-k-sorted-lists/
ProgramCreek: http://www.programcreek.com/2013/02/leetcode-merge-k-sorted-lists-java/
Analysis:
1.Merge Sort.
Using divid and conquer
Runtime: 1 ms, faster than 100.00% of Java online submissions for Merge k Sorted Lists.
2.Priority Queue
Time: O(N*logK), N is # of total nodes, K is # of lists
3.Merge lists two by two.
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
// 1.Merge Sort(Divid and Conquer)
public ListNode mergeKLists(ListNode[] lists) {
if(lists == null || lists.length == 0) return null;
return mergeHelper(lists, 0, lists.length - 1);
}
private ListNode mergeHelper(ListNode[] lists, int start, int end) {
if (start == end) return lists[start];
int mid = start + (end - start) / 2;
ListNode left = mergeHelper(lists, start, mid);
ListNode right = mergeHelper(lists, mid + 1, end);
return mergeTwoLists(left, right);
}
private ListNode mergeTwoLists(ListNode left, ListNode right) {
ListNode dummy = new ListNode(-1);
ListNode p = dummy;
while (left != null && right != null) {
if (left.val < right.val) {
p.next = left;
left = left.next;
} else {
p.next = right;
right = right.next;
}
p = p.next;
}
if (left != null) p.next = left;
if (right != null) p.next = right;
return dummy.next;
}
// 2.PriorityQueue
// public ListNode mergeKLists(ListNode[] lists) {
// if(lists == null || lists.length == 0) return null;
// Queue<ListNode> heap = new PriorityQueue<ListNode>(lists.length, listNodeComparator);
// for(int i = 0; i < lists.length; i++){
// if(lists[i] != null){
// heap.add(lists[i]);
// }
// }
// ListNode dummy = new ListNode(-1);
// ListNode curr = dummy;
// while(!heap.isEmpty()){
// ListNode min = heap.poll();
// curr.next = min;
// curr = curr.next;
// if(min.next != null){
// heap.add(min.next);
// }
// }
// return dummy.next;
// }
// private Comparator<ListNode> listNodeComparator = new Comparator<ListNode>(){
// public int compare(ListNode l1, ListNode l2){
// if(l1 == null) return 1;
// else if(l2 == null) return -1;
// return l1.val - l2.val;
// }
// };
// another way to write, which is simpler.
/**
Time: log(k) * n.
k is number of list and n is number of total elements.
*/
// public ListNode mergeKLists(ListNode[] lists) {
// if (lists == null || lists.length == 0) return null;
// PriorityQueue<ListNode> queue = new PriorityQueue<>((a, b) -> Integer.compare(a.val, b.val));
// for(ListNode node : lists) {
// if (node != null) queue.add(node);
// }
// ListNode dummy = new ListNode(-1);
// ListNode p = dummy; // pointer
// while(!queue.isEmpty()) {
// ListNode curr = queue.poll();
// p.next = curr;
// p = p.next;
// if(curr.next != null) queue.add(curr.next);
// }
// return dummy.next;
// }
// 3.Merge lists two by two (Like merge sort). Referred from JiuZhang.
// public ListNode mergeKLists(ListNode[] lists) {
// if(lists == null || lists.length == 0) return null;
// }
}