public ListNode mergeKLists(ArrayList<ListNode> lists) {
// Start typing your Java solution below
// DO NOT write main() function
ListNode head = null;
ListNode cur = head;
if(lists != null && lists.size() > 0){
while(true){
ListNode min = null;
for(ListNode node : lists){
if(node != null){
if(min == null){
min = node;
}else{
min = min.val >= node.val ? node:min;
}
}
}
if(min == null){
return head;
}
if(head == null){
head = min;
cur = head;
}else{
cur.next = min;
cur = cur.next;
}
ListNode next = min.next;
lists.remove(min);
if(next != null){
lists.add(next);
}
}
}
return head;
}
Merge k Sorted Lists
最新推荐文章于 2022-05-22 19:43:19 发布