LeetCode 23. Merge k Sorted Lists(java)

原创 2018年01月20日 08:43:25

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

解法一:用heap来做,先把k个list的第一个元素放进heap,每次poll出一个后,放进poll出的那个的后一个,直到heap为空。时间复杂度O(mklogk),空间复杂度为O(k).

public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) return null;
        if (lists.length == 1) return lists[0];
        PriorityQueue<ListNode> queue = new PriorityQueue<>(new Comparator<ListNode>() {
            public int compare(ListNode o1, ListNode o2) {
                return o1.val - o2.val;
            }
        });
        for (int i = 0; i < lists.length; i++) {
            if (lists[i] != null) queue.add(lists[i]);
        }
        ListNode dummy = new ListNode(-1), cur = dummy;
        while (!queue.isEmpty()) {
            ListNode temp = queue.poll();
            cur.next = temp;
            cur = temp;
            if (temp.next != null) queue.add(temp.next);
        }
        return dummy.next;
    }

解法二:merge sort的想法,比解法一更优。每次都两两list merge,直到merge成一个list,时间复杂度O((m/2)(k/2 + k/4 + k/8 + …)logk),还是O(kmlogk),但是比方法一有常数量上的优化。空间复杂度为O(1).

public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) return null;
        int begin = 0, end = lists.length - 1;
        while (begin < end) {
            int mid = (begin + end - 1) / 2;
            for (int i = 0; i <= mid; i++) {
                lists[i] = merge2list(lists[i], lists[end - i]);
            }
            end = (begin + end) / 2;
        }
        return lists[0];
    }
    public ListNode merge2list(ListNode l1, ListNode l2) {
        if (l1 == null && l2 == null) return null;
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        ListNode dummy = new ListNode(-1), cur = dummy;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                cur.next = l1;
                cur = l1;
                l1 = l1.next;
            } else {
                cur.next = l2;
                cur = l2;
                l2 = l2.next;
            }
        }
        if (l1 != null) cur.next = l1;
        if (l2 != null) cur.next = l2;
        return dummy.next;
    }

C++的背影——C++之父Bjarne Stroustrup印象

C++的背影——C++之父Bjarne Stroustrup印象左轻侯2002.11.4  热爱C++的朋友请不要误会,我并不是在暗示“C++已经日薄西山”,或者任何类似的意思。从语义上来说,C++作...
  • qinghou
  • qinghou
  • 2003-01-06 10:41:00
  • 4201

【LeetCode】23. Merge k Sorted Lists 基于Java的解法

23. Merge k Sorted Lists Total Accepted: 88102 Total Submissions: 372066 Difficulty: Hard Merge k...
  • Jin_Kwok
  • Jin_Kwok
  • 2016-06-04 00:46:59
  • 1814

LeetCode 23 Merge k Sorted Lists(合并K个已排序链表)

翻译合并K个已排序的链表,并且将其排序并返回。 分析和描述其复杂性。原文Merge k sorted linked lists and return it as one sorted list. An...
  • NoMasp
  • NoMasp
  • 2015-11-12 17:30:10
  • 3556

[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Java/Python)

[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-03-05 11:19:11
  • 2634

leetcode_效率题解_23. Merge k Sorted Lists(合并k个有序链表)

相关题解: leetcode_效率题解_[python/C++]_21. Merge Two Sorted Lists(合并2个有序链表)题目链接 【题目】 Merge k sorted lin...
  • huangmx1995
  • huangmx1995
  • 2016-11-22 22:09:45
  • 353

LeetCode 23: Merge K Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. ...
  • sunao2002002
  • sunao2002002
  • 2015-05-31 02:04:42
  • 3594

[Leetcode]23. Merge k Sorted Lists @python

题目Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity....
  • qian2729
  • qian2729
  • 2016-01-16 15:32:14
  • 662

LeetCode 23 Merge k Sorted Lists (C,C++,Java,Python)

Problem: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its c...
  • runningtortoises
  • runningtortoises
  • 2015-05-11 15:39:00
  • 1062

LeetCode Merge k Sorted Lists 解题报告

合并K个已排序的数组,并分析整个算法的复杂度。 最朴素的方法TLE,借鉴归并排序的算法顺利AC,算法时间复杂度:NlogK...
  • worldwindjp
  • worldwindjp
  • 2014-10-11 15:23:24
  • 8408

Merge k Sorted Lists -- LeetCode

原题链接: http://oj.leetcode.com/problems/merge-k-sorted-lists/  这道题目在分布式系统中非常常见,来自不同client的sorted list要...
  • linhuanmars
  • linhuanmars
  • 2014-02-25 09:45:41
  • 26833
收藏助手
不良信息举报
您举报文章:LeetCode 23. Merge k Sorted Lists(java)
举报原因:
原因补充:

(最多只允许输入30个字)