一題多解答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 循環(huán),需要進行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;
        }
    };
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,927評論 0 33
  • //leetcode中還有花樣鏈表題,這里幾個例子,冰山一角 求單鏈表中結點的個數----時間復雜度O(n)這是最...
    暗黑破壞球嘿哈閱讀 1,659評論 0 6
  • 《故事》這本書里的劇本分析和編劇技巧讓我大開眼界,獲益匪淺。也讓我得以用一個更專業(yè)的角度一看看穿了自己的漫畫:...
    楊一同學閱讀 224評論 0 1
  • 人生僅一需珍惜, 莫在失時長嘆息。 落葉方知秋可貴, 失去方感倍珍惜
    化念閱讀 145評論 0 0
  • 禹禹獨行冬雨中,街上行人步履匆。 暖閣豈知冰雪意。薄云濃霧寒霜兇。 紫蘿綠藤枯葉崩,昏鴉南去老巢空。 閉門添衣重執(zhí)...
    木木歸一閱讀 630評論 0 1

友情鏈接更多精彩內容