|  | 
 
| 
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册    
 用二叉堆来优化:
 
 
 复制代码/**
 * 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 <ListNode> queue = new PriorityQueue<ListNode>(lists.length, (a,b) -> a.val - b.val);
        
        for(ListNode e: lists){
            
            if(e != null){
                
                queue.add(e);
                
            }
            
        }
        
        ListNode ptr = new ListNode(0);
        
        ListNode head = ptr;
        
        while(!queue.isEmpty()){
            
            ptr.next = queue.poll();
            
            ptr = ptr.next;
            
            if(ptr.next != null){
                
                queue.add(ptr.next);
                
            }
            
            
        }
        
        return head.next;
        
    }
    
}
    
 
 用 divide and conquer:
 
 
 
 
 
 
 | 
 |