-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmerge-k-sorted-lists.java
88 lines (79 loc) · 2.23 KB
/
merge-k-sorted-lists.java
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
// merge sort style
public static void main (String[] args) throws java.lang.Exception
{
return mergeLists(lists, 0, lists.length-1);
}
public static ListNode mergeTwoLists (ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
if (l1.val > l2.val) {
l2.next = mergeTwoLists(l1.next, l2);
return l2;
} else {
l1.next = mergeTwoLists(l1, l2.next);
return l1;
}
}
public static ListNode mergeLists(ListNode[] lists, int left, int right) {
if (left > right) return null;
if (left == right) return lists[left];
int len = right - left + 1;
return mergeTwoLists(mergeLists(lists, 0, len/2), mergeLists(lists, len/2+1, len));
}
// using priority queue
public ListNode mergeKLists(List<ListNode> lists) {
PriorityQueue<ListNode> p = new PriorityQueue<lists.size(), new Comparator<ListNode>() {
public int compareTo(ListNode other) {
return this.val - other.val;
}
}
ListNode head = new ListNode(0);
ListNode dummy = head;
for (ListNode listNode : lists) {
if (listNode != null) p.add(listNode);
}
while (!p.isEmpty()) {
dummy.next = p.poll();
dummy = dummy.next;
if (dummy.next != null)
p.add(dummy.next);
}
return head.next;
}
// for loop finding smallest ListNode in an array with no priority queue
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) return null;
ListNode dummy = new ListNode(Integer.MAX_VALUE), cur = dummy;
while (cur != null) {
ListNode smallest = dummy;
int index = 0;
for (int i = 0; i < lists.length; i++) {
if (lists[i] != null && smallest.val > lists[i].val) {
smallest = lists[i];
index = i;
}
}
if (smallest != dummy) cur.next = smallest;
cur = cur.next;
if (lists[index] != null) lists[index] = lists[index].next;
}
return dummy.next;
}
}
}