23. Merge k Sorted Lists via Java

有序列表merge核心思想-->谁小就选谁加入结果

所以这道题的最核心问题也就找到了,就是要找到任意时刻的最小元素。所以需要维持一个数据结构,随时返回其中最小的元素,也就是最堆

然后这道题的难点就变成了写最小堆的comparator

下方代码中有两种我比较喜欢的方式

class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if(lists==null || lists.length==0){
            return null;
        }
        ListNode res = new ListNode(0);
        ListNode cur = res;
        // lambda instead of anonymous comparator class
        PriorityQueue<ListNode> pq = new PriorityQueue<>((ListNode l1, ListNode l2)->l1.val-l2.val);
        // classic comparator
        // PriorityQueue<ListNode> pq = new PriorityQueue<>(new Comparator<ListNode>(){
        //     @Override 
        //     public int compare(ListNode i, ListNode j) { 
        //         if(i.val==j.val){ 
        //             return 0;
        //         }
        //         return i.val<j.val?-1:1;
        //     }});
        for(int i = 0; i<lists.length; i++){
            if(lists[i]!=null){
                pq.add(lists[i]);
            }
        }
        while(!pq.isEmpty()){
            cur.next = pq.poll();
            if(cur.next.next!=null){
                pq.offer(cur.next.next);
            }
            cur = cur.next;
        }
        cur.next = null;
        return res.next;
    }
}

time: 对二叉堆进行n次poll()操作,每次复杂度为O(lgk),其中k为初始链表数组中链表数,即最终结果为O(nlgk)

space:二叉堆:O(k) 返回结果O(n) 综合O(n+k)

猜你喜欢

转载自www.cnblogs.com/zg1005/p/11902327.html