-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_23.java
64 lines (58 loc) · 1.7 KB
/
LC_23.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
import java.util.List;
public class LC_23 {
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;
}
}
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) return null;
if (lists.length == 1)
return lists[0];
ListNode dummy = new ListNode();
ListNode p = dummy;
ListNode p1 = lists[0];
ListNode p2 = lists[1];
while (p1 != null && p2 != null) {
if (p1.val < p2.val) {
p.next = p1;
p1 = p1.next;
} else {
p.next = p2;
p2 = p2.next;
}
p = p.next;
}
if (p1 == null) p.next = p2;
else p.next = p1;
for (int i = 2; i < lists.length; i++) {
if (lists[i] == null) continue;
ListNode dummy1 = new ListNode();
ListNode cur = dummy1;
ListNode p3 = dummy.next;
ListNode p4 = lists[i];
while (p4 != null && p3 != null) {
if (p3.val < p4.val) {
cur.next = p3;
p3 = p3.next;
} else {
cur.next = p4;
p4 = p4.next;
}
cur = cur.next;
}
if (p3 == null) cur.next = p4;
else cur.next = p3;
dummy = dummy1;
}
return dummy.next;
}
}