一题多解答Merge K sorted List

三种解法

  • Solution 1 Merge two sorted Lists
    两两归并
    Time: O(nlogk)
    每个单独的listNode参与到归并的话是O(1)(比如一个长度为m的ListNode和一个长度n的ListNode合并需要时间为O(m + n). 而且每一个node最多参与logk次归并,所以最坏情况需要O(Nlogk)时间复杂度
/**
 * 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;
        }
        List<ListNode> asLists = Arrays.asList(lists);
        while (asLists.size() > 1){
            List<ListNode> newLists = new ArrayList<ListNode>();
            for (int i = 0; i + 1 < asLists.size(); i+=2){
                ListNode newNode = mergeTwoSortedLists(asLists.get(i), asLists.get(i+1));
                newLists.add(newNode);
            }           
            if (asLists.size() % 2 == 1){
                newLists.add(asLists.get(asLists.size() - 1));
            }
            asLists = newLists;
        }
        return asLists.get(0);
    }

    private ListNode mergeTwoSortedLists(ListNode l1, ListNode l2){
        if (l1 == null){
            return l2;
        } 
        if (l2 == null){
            return l1;
        }
        ListNode dummy = new ListNode(0);
        ListNode curt = dummy;
        while (l1 != null && l2 != null){
            if (l1.val < l2.val){
                curt.next = l1;
                l1 = l1.next;
            } else {
                curt.next = l2;
                l2 = l2.next;
            }
            curt = curt.next;
        }
        while(l1 != null){
            curt.next = l1;
            l1 = l1.next;
            curt = curt.next;
        }
        while (l2 != null){
            curt.next = l2;
            l2 = l2.next;
            curt = curt.next;
        }
        return dummy.next;
    }
}
  • Solution 2 minHeap
    时间复杂度:O(Nlogk)
    while (!pq.isEmpty()){要进行n次,n为所有node的个数;而每一次while 循环,需要进行poll(), offer()操作,而每一次poll() or offer()需要logk.所以时间复杂度是O(NlogK)
/**
 * 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;
        }
        int k = lists.length;
        ListNode dummy = new ListNode(0);
        ListNode curt = dummy;
        PriorityQueue<ListNode> pq = new PriorityQueue<>(k, cmp);
        for (ListNode list : lists){
            if (list != null){
                pq.offer(list);
            }
        }
        while (!pq.isEmpty()){
            ListNode top = pq.poll();
            curt.next = top;
            if (top.next != null){
                pq.offer(top.next);
            }
            curt = curt.next;
        }        
        return dummy.next;
    }
    
    private Comparator<ListNode> cmp = new Comparator<ListNode>(){
        public int compare(ListNode l1, ListNode l2){
            return l1.val - l2.val;
        }
    };
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,932评论 0 33
  • //leetcode中还有花样链表题,这里几个例子,冰山一角 求单链表中结点的个数----时间复杂度O(n)这是最...
    暗黑破坏球嘿哈阅读 1,671评论 0 6
  • 《故事》这本书里的剧本分析和编剧技巧让我大开眼界,获益匪浅。也让我得以用一个更专业的角度一看看穿了自己的漫画:...
    杨一同学阅读 229评论 0 1
  • 人生仅一需珍惜, 莫在失时长叹息。 落叶方知秋可贵, 失去方感倍珍惜
    化念阅读 145评论 0 0
  • 禹禹独行冬雨中,街上行人步履匆。 暖阁岂知冰雪意。薄云浓雾寒霜凶。 紫萝绿藤枯叶崩,昏鸦南去老巢空。 闭门添衣重执...
    木木归一阅读 631评论 0 1

友情链接更多精彩内容