/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if(lists==null||lists.length==0)
return null;
PriorityQueue<Integer> queue=new PriorityQueue();
for(ListNode node:lists){
while(node!=null){
queue.add(node.val);
node=node.next;
}
}
ListNode dummy=new ListNode(0);
ListNode p=dummy;
while(!queue.isEmpty()){
ListNode temp=new ListNode(queue.poll());
p.next=temp;
p=p.next;
}
return dummy.next;
}
}
利用优先级队列更好些。