leetcode 23 merge K soted lists
作者:互联网
返回成一个排好序的list
K 个,第一个NODE,开始比较;
每个LIST 第一个NODE ,作为比较开始,这样如此;
prioty quee,最小数poll出来,等到其变成空,就得到
public LsitNode mergeKList(ListNode[] lists){
if(lists==null||lists.length==0) return null;// corner case
Queue<ListNode>pq= new PriorityQueue(lists.length,(ListNode a, ListNode b)-> (a.val-b.val));
//put each list node 1 into pq
for(int i=0;i<lists.length;i++){
if(lists[i]!=null) pq.offer(lists[i]);
}
ListNode dummy= new ListNode(0);
ListNode tail= dummy;
while(!pq.isEmpty()){
ListNode head= pq.poll();
tail.next=head;
tai=head;
if(head.next !=null) pq.offer(head.next);
}
return dummy.next;
}}
标签:head,pq,ListNode,lists,next,merge,soted,null,leetcode 来源: https://www.cnblogs.com/LLflag1/p/16468584.html