一题多解答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;
        }
    };
}
    原文作者:greatfulltime
    原文地址: https://www.jianshu.com/p/bd6325f9c1b8
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞