-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP23.java
More file actions
153 lines (130 loc) · 4.66 KB
/
P23.java
File metadata and controls
153 lines (130 loc) · 4.66 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import java.util.*;
/*
23. 合并K个排序链表
https://leetcode-cn.com/problems/merge-k-sorted-lists/
合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
示例:
输入:
[
1->4->5,
1->3->4,
2->6
]
输出: 1->1->2->3->4->4->5->6
*/
public class P23 {
public static void main(String[] args) {
ListNode l1 = stringToListNode("[1, 4, 5]");
ListNode l2 = stringToListNode("[1, 3, 4]");
ListNode l3 = stringToListNode("[2, 6]");
prettyPrintLinkedList(new Solution_heap().mergeKLists(new ListNode[] {l1, l2, l3}));
//prettyPrintLinkedList(new Solution().mergeKLists(new ListNode[] {l1, l2, l3}));
}
public static class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
static class Solution {
// 两两合并
// O(Nlogk) k 为总链表个数,N 为总元素个数
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) return null;
return binaryMerge(lists, 0, lists.length - 1);
}
public ListNode binaryMerge(ListNode[] list, int left, int right) {
if (left >= right) return list[left];
int mid = left + right >>> 1;
ListNode l0 = binaryMerge(list, left, mid);
ListNode l1 = binaryMerge(list, mid + 1, right);
return mergeTwoLists(l0, l1);
}
// 21. 合并两个有序链表
// https://leetcode-cn.com/problems/merge-two-sorted-lists/
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null && l2 == null) {
return null;
}
ListNode first = new ListNode(0);
ListNode t = first;
while(l1 != null && l2 != null) {
if (l1.val < l2.val) {
t.next = l1;
l1 = l1.next;
} else {
t.next = l2;
l2 = l2.next;
}
t = t.next;
}
// 链表最后一个
t.next = (l1 != null) ? l1 : l2;
return first.next;
}
}
static class Solution_heap {
//利用优先队列, 该数据结构用到的是堆排序,
// 所以对总链表个数为 k 的复杂度为 logk, 总元素为个数为 N 的话, 其时间复杂度也为 O(Nlogk)
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length == 0) return null;
Queue<ListNode> queue = new PriorityQueue<>(lists.length, (o1, o2) -> {
// if (o1.val < o2.val) return -1;
// else if (o1.val == o2.val) return 0;
// else return 1;
return o1.val - o2.val;
});
ListNode node = new ListNode(0), tmp = node;
for (ListNode l : lists) {
if (l != null) queue.add(l);
}
while (!queue.isEmpty()) {
//tmp.next = queue.poll();
ListNode head = queue.poll();
tmp.next = head;
tmp = head;
if (tmp.next != null) {
queue.add(tmp.next);
// 进一个出一个
}
}
return node.next;
}
}
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
}
String[] parts = input.split(",");
int[] output = new int[parts.length];
for(int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}
public static ListNode stringToListNode(String input) {
// Generate array from the input
int[] nodeValues = stringToIntegerArray(input);
// Now convert that list into linked list
ListNode dummyRoot = new ListNode(0);
ListNode ptr = dummyRoot;
for(int item : nodeValues) {
ptr.next = new ListNode(item);
ptr = ptr.next;
}
return dummyRoot.next;
}
public static void prettyPrintLinkedList(ListNode node) {
while (node != null && node.next != null) {
System.out.print(node.val + "->");
node = node.next;
}
if (node != null) {
System.out.println(node.val);
} else {
System.out.println("Empty LinkedList");
}
}
}