`
frank-liu
  • 浏览: 1666038 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

leetcode: Merge k Sorted Lists

 
阅读更多

问题描述:

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

原问题链接:https://leetcode.com/problems/merge-k-sorted-lists/

 

问题分析

    这个问题看起来比较复杂,但是结合我前面讨论heap sortpriority queue的文章分析的话,则会发现其实这是一个固定的套路。 

    这个问题的解决思路基本如下。首先用priority queue将整个list里面的每个链表的第一个元素都放入到里面。然后每次从queue里取它的第一个元素。按照priority queue的定义,它最前面的元素必然是最小的元素。然后再判断这个取出来的元素后面是否还有别的元素。有的话则将它后面的那个元素加入到priority queue中间。

    因为要返回一个排序的链表,这里可以创建一个链表的节点,然后用一个节点每次指向从queue里取出的那个元素。然后再往后移动指针。

    这个问题有一个取巧的地方。就是它只适用于链表的情况。因为这里每次取到了队列头部的元素可以顺便拿到它后面的元素。而如果是ArrayList之类的结构则不能得到这样的信息。问题就会要复杂很多。

    总之,根据前面的讨论可以得到如下的代码:

 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if(lists == null || lists.length == 0) return null;
        if(lists.length == 1) return lists[0];
        Queue<ListNode> queue = new PriorityQueue<>(lists.length, new Comparator<ListNode>() {
            @Override
            public int compare(ListNode l1, ListNode l2) {
                return l1.val - l2.val;
            }});
        for(ListNode node : lists) {
            if(node != null) queue.add(node);
        }
        ListNode pre = new ListNode(0);
        ListNode head = pre;
        while(!queue.isEmpty()) {
            ListNode temp = queue.poll();
            head.next = temp;
            head = head.next;
            if(temp.next != null) queue.add(temp.next);
        }
        return pre.next;
    }
}

    假设list的长度为k的话,上述代码实现的时间复杂度为O(logK * N)。

 

总结

     这个问题和前面的priority queue,堆排序它们有密切的关系。里面的各种推导和变换有不少值得深究的地方。

 

 

分享到:
评论

相关推荐

    LeetCode Merge 2 Sorted Lists解决方案

    LeetCode Merge 2 Sorted Lists解决方案

    leetcode中文版-LeetCode:力码

    leetcode中文版 LeetCode/Cpp 本人刷题记录在此,包含题意理解与算法思路,包含在Cpp文件内部注释,后续会持续更新。 有不懂的可以联系ji648513181,同时也欢迎志同道合O的朋友一起合作更新。 已更新剑指Offer答案...

    leetcode分类-leetcode:leetcode问题的代码

    leetcode 分类leetcode 问题分类 leetcode代码仓库,我的解题思路写在我的博客里: leetcode 代码库,我博客上的解题思路: mkdir 列表: 大批 #1:Two Sum #4:Median ...Sorted ...#23:Merge k Sorted Lists

    MergeTwoSortedLinkedList.java

    【Leetcode】Merge Two Sorted Lists

    leetcode2-Leetcode:Leetcode_answer

    leetcode 2 Leetcode答案集 关于项目: 本项目包含本人LeetCode解题的答案,全部将由JavaScript语言进行解答。并会在每个题目的文件夹中添加相关的思路解析。...Merge Two Sorted Lists JavaScript O(n)

    leetcode跳跃-leetcode:leetcode一天一次

    leetcode 跳跃 leetcode 动态规划,背包问题 01背包问题:416. Partition Equal Subset Sum 最长回文:5. Longest Palindromic ...Merge ...Sorted Lists 回文 双指针判断回文:680. 验证回文字符串 Ⅱ

    程序员面试宝典LeetCode刷题手册

    第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters ...23. Merge k Sorted Lists 24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group 26. Remove Dupli

    leetcode答案-LeetCode:Swift中的LeetCode

    Merge Two Sorted Lists Easy #26 Remove Duplicates from Sorted Array Easy #27 Remove Element Easy #35 Search Insert Position Easy #38 Count and Say Easy #53 Maximum Subarray Easy #66 Plus One Easy #70 ...

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode leetcode ...Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3

    leetcode添加元素使和等于-leetcode:我的leetcode解决方案

    leetcode添加元素使和等于 经验教训 一定要吧功能尽量细化为函数,这样一者做题思路比较清晰,二者可以在某些情况下直接return值。 如果输入的形式是一个序列,则可以想想:分治、动规、贪婪,一般不建议搜索,因为...

    leetcode2sumc-LeetCode:LeetCode的一些题目

    Merge Two Sorted Lists 83 Easy Remove Duplicates from Sorted List 141 Easy Linked List Cycle 160 Easy Intersection of Two Linked Lists 203 Easy Remove Linked List Elements no 206 Easy Reverse Linked ...

    leetcode双人赛-LeetCode:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...

    leetcode中文版-LeetCode:LeetcodeC++/Java

    leetcode中文版 LeetCode # Title Chinese Tag Solution 1 Two Sum 两数之和 array,hash 2 Add Two Numbers 两数相加 math 3 Longest Substring ...Sorted ...Merge Two Sorted Lists 合并两个有序链表 lin

    leetcode被墙-leetcode:leetcode笔记

    easy:21.Merge_Two_Sorted_Lists return a or b 首先我是第一次见到这种写法,查看了官方文档,引用如下: The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is ev

    leetcode中325题python-leetcode:leetcode

    merge-two-sorted-lists 82 删除排序链表中的重复元素 II remove-duplicates-from-sorted-list ii 83 删除排序链表中的重复元素 remove-duplicates-from-sorted-list 86 分隔链表 partition-list 92 反转链表 II ...

    leetcode跳跃-leetcode:leetcode解题之路

    leetcode 跳跃 leetcode 按题型标签,记录...合并K个排序链表](./Heap/merge-k-sorted-lists.md) String 字符串 [0006 Z字形变换](./String/zigzag-conversion.md) [0030 串联所有单词的子串](./String/substring

    leetcodepython001-LeetCode:力码

    leetcode Python ...021_Merge_Two_Sorted_Lists 2021 年 6 月 10 日 C# 008 125_Valid_Palindrome 2021 年 6 月 12 日 C# 009 412_FizzBu​​zz 2021 年 6 月 22 日 C# 中等的 # 问题 完成日期 语

    lrucacheleetcode-leetcode-journal:记录所有LeetCode挑战的存储库

    lru缓存leetcode 力扣日记 欢迎来到我的 LeetCode 期刊库。 我的每个问题都将包括一个初始计划...k Sorted Lists (Hard) 31. Next Permutation (Medium) 34. Find First and Last Position of Element in Sorted Arr

    多线程leetcode-leetcode-java:leetcode上的题解,基于java语言

    多线程 leetcode 前言 每天刷点leetcode,基于java语言实现。 leetcode上难度分三档:easy,medium,hard. 如下: easy medium ...Merge k Sorted Lists Reverse Nodes in k-Group Trapping Rain Water

    validnumberleetcode自动机-LearingTripofNoah:我自己的学习之旅,天天学习至此方休

    Lists(合并 k 个排序链表) 英文版: 中文版: Task2(8.5-8.9) 3.栈 用数组实现一个顺序栈 用链表实现一个链式栈 编程模拟实现一个浏览器的前进、后退功能 4.队列 用数组实现一个顺序队列 用链表实现一个链式队列...

Global site tag (gtag.js) - Google Analytics