package main.java.demo23;
import main.java.ListNode;
public class MergeKLists {
ListNode head = new ListNode();
ListNode current = head;
public ListNode mergeKLists_1(ListNode[] lists) {
int n = lists.length;
Integer result = null;
int resultIndex = 0;
while (true) {
for (int i = 0; i < n; i++) {
if (lists[i] != null) {
if (result == null) {
result = lists[i].val;
resultIndex = i;
} else if (lists[i].val < result) {
result = lists[i].val;
resultIndex = i;
}
}
}
if (result == null) {
return head.next;
}
result = null;
current.next = lists[resultIndex];
current = current.next;
lists[resultIndex] = lists[resultIndex].next;
}
}
}
|