-
Notifications
You must be signed in to change notification settings - Fork 14
/
23-moran991231.java
54 lines (50 loc) · 1.15 KB
/
23-moran991231.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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public static ListNode endOf(ListNode node) {
while (node.next != null) {
node = node.next;
}
return node;
}
public static ListNode mergeKLists(ListNode[] lists) {
int len = lists.length;
ListNode head = null;
ListNode[] nodes = new ListNode[20_001];
ListNode temp;
for (int i = 0; i < len; i++) {
while (lists[i] != null) {
temp = lists[i];
lists[i] = temp.next; // pop head node in lists[i]
temp.next = nodes[temp.val + 10000]; // push
nodes[temp.val + 10000] = temp;
}
}
// find head
int start;
for (start = 0; start < 20001; start++) {
if (nodes[start] != null) {
head = nodes[start];
break;
}
}
if (head != null)
temp = endOf(head);
else temp = null;
//merge
for(int i=start+1; i<20001; i++) {
if (nodes[i]== null) continue;
temp.next=nodes[i];
temp = endOf(nodes[i]);
}
return head;
}
}