|
1 | 1 | package algorithm.pointers.merge; |
2 | 2 |
|
3 | 3 | import datastrcture.ListNode; |
| 4 | + |
4 | 5 | import java.util.*; |
5 | 6 |
|
6 | 7 | /** |
7 | 8 | * You are given an array of k linked lists, each is sorted in ascending order. |
8 | 9 | * Merge all the linked-lists into one sorted linked-list and return it. |
9 | 10 | */ |
10 | 11 | public class MergeKSortedList { |
11 | | - // Method 1: K Pointers Merge with MinHeap |
12 | | - // Time: O(nK*logK), Space: O(K) |
13 | | - public ListNode mergeKSortedList(ListNode[] lists) { |
14 | | - if (lists == null || lists.length == 0) { |
15 | | - return null; |
16 | | - } |
17 | | - |
18 | | - int k = lists.length; |
19 | | - // keep k pointers to merge k list, each time move next the list with smallest |
20 | | - // thus we can use a min heap with size k to compare the k pointers |
21 | | - PriorityQueue<ListNode> minHeap = new PriorityQueue<>(k, (n1, n2) -> n1.val - n2.val); |
22 | | - // initialize: add k head to the heap |
23 | | - for (ListNode head : lists) { |
24 | | - if (head != null) { // corner case: the k list may have null list |
25 | | - minHeap.offer(head); |
26 | | - } |
| 12 | + // Method 1: K Pointers Merge with MinHeap |
| 13 | + // Time: O(nK*logK), Space: O(K) |
| 14 | + public ListNode mergeKSortedList(ListNode[] lists) { |
| 15 | + if (lists == null || lists.length == 0) { |
| 16 | + return null; |
| 17 | + } |
| 18 | + |
| 19 | + int k = lists.length; |
| 20 | + // keep k pointers to merge k list, each time move next the list with smallest |
| 21 | + // thus we can use a min heap with size k to compare the k pointers |
| 22 | + PriorityQueue<ListNode> minHeap = new PriorityQueue<>(k, (n1, n2) -> n1.val - n2.val); |
| 23 | + // initialize: add k head to the heap |
| 24 | + for (ListNode head : lists) { |
| 25 | + if (head != null) { // corner case: the k list may have null list |
| 26 | + minHeap.offer(head); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + ListNode dummy = new ListNode(0); |
| 31 | + ListNode cur = dummy; |
| 32 | + |
| 33 | + // each add the smallest node to the new list, and add next node into the heap |
| 34 | + while (!minHeap.isEmpty()) { |
| 35 | + ListNode node = minHeap.poll(); |
| 36 | + if (node.next != null) { // added all the nodes of one list |
| 37 | + minHeap.offer(node.next); |
| 38 | + } |
| 39 | + cur.next = node; |
| 40 | + node.next = null; |
| 41 | + cur = node; |
| 42 | + } |
| 43 | + |
| 44 | + return dummy.next; |
27 | 45 | } |
28 | 46 |
|
29 | | - ListNode dummy = new ListNode(0); |
30 | | - ListNode cur = dummy; |
31 | | - |
32 | | - // each add the smallest node to the new list, and add next node into the heap |
33 | | - while (!minHeap.isEmpty()) { |
34 | | - ListNode node = minHeap.poll(); |
35 | | - if (node.next != null) { // added all the nodes of one list |
36 | | - minHeap.offer(node.next); |
37 | | - } |
38 | | - cur.next = node; |
39 | | - node.next = null; |
40 | | - cur = node; |
| 47 | + // Method 2: Recursive Binary Reduction, Divide and Conquer |
| 48 | + // Time: O(nK*logK), Space: O(K) for call stack |
| 49 | + public ListNode mergeKSortedList1(ListNode[] lists) { |
| 50 | + if (lists == null || lists.length == 0) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + return merge(lists, 0, lists.length - 1); |
41 | 54 | } |
42 | 55 |
|
43 | | - return dummy.next; |
44 | | - } |
45 | | - |
46 | | - // Method 2: Recursive Binary Reduction, Divide and Conquer |
47 | | - // Time: O(nK*logK), Space: O(K) for call stack |
48 | | - public ListNode mergeKSortedList1(ListNode[] lists) { |
49 | | - if (lists == null || lists.length == 0) { |
50 | | - return null; |
51 | | - } |
52 | | - return merge(lists, 0, lists.length - 1); |
53 | | - } |
54 | | - |
55 | | - // return merged lists that range from start to end |
56 | | - private ListNode merge(ListNode[] lists, int start, int end) { |
57 | | - // base case |
58 | | - if (start == end) { // the list itself |
59 | | - return lists[start]; |
| 56 | + // return merged lists that range from start to end |
| 57 | + private ListNode merge(ListNode[] lists, int start, int end) { |
| 58 | + // base case |
| 59 | + if (start == end) { // the list itself |
| 60 | + return lists[start]; |
| 61 | + } |
| 62 | + |
| 63 | + int mid = start + (end - start) / 2; |
| 64 | + ListNode left = merge(lists, start, mid); |
| 65 | + ListNode right = merge(lists, mid + 1, end); |
| 66 | + return mergeTwoList(left, right); |
60 | 67 | } |
61 | 68 |
|
62 | | - int mid = start + (end - start) / 2; |
63 | | - ListNode left = merge(lists, start, mid); |
64 | | - ListNode right = merge(lists, mid + 1, end); |
65 | | - return mergeTwoList(left, right); |
66 | | - } |
67 | | - |
68 | | - // Method 3: Iterative Binary Reduction |
69 | | - // Time: O(nK*logK), Space: O(1) |
70 | | - public ListNode mergeKSortedList2(ListNode[] lists) { |
71 | | - if (lists == null || lists.length == 0) { |
72 | | - return null; |
| 69 | + // Method 3: Iterative Binary Reduction |
| 70 | + // Time: O(nK*logK), Space: O(1) |
| 71 | + public ListNode mergeKSortedList2(ListNode[] lists) { |
| 72 | + if (lists == null || lists.length == 0) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + int k = lists.length; |
| 77 | + // reuse the list node array |
| 78 | + for (int interval = 1; interval < k; interval *= 2) { |
| 79 | + for (int i = 0; i < (k - interval); i += interval * 2) { |
| 80 | + lists[i] = mergeTwoList(lists[i], lists[i + interval]); |
| 81 | + } |
| 82 | + } |
| 83 | + return lists[0]; |
73 | 84 | } |
74 | 85 |
|
75 | | - int k = lists.length; |
76 | | - // reuse the list node array |
77 | | - for (int interval = 1; interval < k; interval *= 2) { |
78 | | - for (int i = 0; i < (k - interval); i += interval * 2) { |
79 | | - lists[i] = mergeTwoList(lists[i], lists[i + interval]); |
80 | | - } |
81 | | - } |
82 | | - return lists[0]; |
83 | | - } |
84 | | - |
85 | | - // Method 4: Iterative Merge Two List |
86 | | - // Time: O(nK^2), Space: O(1) |
87 | | - public ListNode mergeKSortedList3(ListNode[] lists) { |
88 | | - if (lists == null || lists.length == 0) { |
89 | | - return null; |
| 86 | + // Method 4: Iterative Merge Two List |
| 87 | + // Time: O(nK^2), Space: O(1) |
| 88 | + public ListNode mergeKSortedList3(ListNode[] lists) { |
| 89 | + if (lists == null || lists.length == 0) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + ListNode merged = lists[0]; |
| 94 | + // for loop can cover the case that length == 1 |
| 95 | + // if length = 1, the for loop will be i = 1 < 1, will not enter |
| 96 | + for (int i = 1; i < lists.length; i++) { |
| 97 | + merged = mergeTwoList(lists[i], merged); |
| 98 | + } |
| 99 | + |
| 100 | + return merged; |
90 | 101 | } |
91 | 102 |
|
92 | | - ListNode merged = lists[0]; |
93 | | - // for loop can cover the case that length == 1 |
94 | | - // if length = 1, the for loop will be i = 1 < 1, will not enter |
95 | | - for (int i = 1; i < lists.length; i++) { |
96 | | - merged = mergeTwoList(lists[i], merged); |
97 | | - } |
98 | | - |
99 | | - return merged; |
100 | | - } |
101 | | - |
102 | | - private ListNode mergeTwoList(ListNode head1, ListNode head2) { |
103 | | - ListNode dummy = new ListNode(0); |
104 | | - ListNode cur = dummy; |
105 | | - |
106 | | - while (head1 != null && head2 != null) { |
107 | | - if (head1.val < head2.val) { |
108 | | - cur.next = head1; |
109 | | - head1 = head1.next; |
110 | | - } else { |
111 | | - cur.next = head2; |
112 | | - head2 = head2.next; |
113 | | - } |
114 | | - cur = cur.next; |
115 | | - } |
116 | | - |
117 | | - if (head1 != null) { |
118 | | - cur.next = head1; |
119 | | - } |
120 | | - if (head2 != null) { |
121 | | - cur.next = head2; |
| 103 | + private ListNode mergeTwoList(ListNode head1, ListNode head2) { |
| 104 | + ListNode dummy = new ListNode(0); |
| 105 | + ListNode cur = dummy; |
| 106 | + |
| 107 | + while (head1 != null && head2 != null) { |
| 108 | + if (head1.val < head2.val) { |
| 109 | + cur.next = head1; |
| 110 | + head1 = head1.next; |
| 111 | + } else { |
| 112 | + cur.next = head2; |
| 113 | + head2 = head2.next; |
| 114 | + } |
| 115 | + cur = cur.next; |
| 116 | + } |
| 117 | + |
| 118 | + if (head1 != null) { |
| 119 | + cur.next = head1; |
| 120 | + } |
| 121 | + if (head2 != null) { |
| 122 | + cur.next = head2; |
| 123 | + } |
| 124 | + return dummy.next; |
122 | 125 | } |
123 | | - return dummy.next; |
124 | | - } |
125 | 126 | } |
0 commit comments