您的解决方案可能是最快的解决方案。SortedList的插入成本为log(n),因此最终得到M log(M)(其中M是列表的总大小)。
将它们添加到一个列表并进行排序(虽然更易于阅读)仍然是M log(M)。
您的解决方案就是M。
您可以通过调整结果列表的大小并使用对最低列表的引用而不是布尔值来稍微整理一下代码。
public static <T extends Comparable<? super T>> List<T> merge(Set<List<T>> lists) { int totalSize = 0; // every element in the set for (List<T> l : lists) { totalSize += l.size(); } List<T> result = new ArrayList<T>(totalSize); List<T> lowest; while (result.size() < totalSize) { // while we still have something to add lowest = null; for (List<T> l : lists) { if (! l.isEmpty()) { if (lowest == null) { lowest = l; } else if (l.get(0).compareTo(lowest.get(0)) <= 0) { lowest = l; } } } result.add(lowest.get(0)); lowest.remove(0); } return result;}如果确实很特别,请使用List对象作为输入,并且最低的对象可以初始化为list.get(0),并且可以跳过null检查。



