• [LeetCode]23. Merge k Sorted Lists


    题目

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

    测试案例

    Input:
    [
      1->4->5,
      1->3->4,
      2->6
    ]
    Output: 1->1->2->3->4->4->5->6
    

    思路

    使用最小堆进行多路归并,堆中存放的元素是每一个有序链表。

    排序的比较方法是链表头节点的值。

    每次从堆中取出最小值后,取出头结点。当链表剩余部分非空时,就将其放入堆中。将头结点放入结果队列的尾部。

    具体实现

    • 由于 java 中有数据结构 PriorityQueue(优先级队列),所以利用它来实现。创建优先级队列时,传入一个比较器即可。

    • 然后依次将每个非空链表放入队列中。

    • 接下来,只要队列非空,就从中取出最小值的链表。取出头结点,剩余部分非空时,放回队列,将头结点放入结果队列尾部。

    • 优先级队列为空时,将结果队列尾节点的 next 置为空,注意,此时 tail 可能为空。需要先判断。

    代码如下

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode mergeKLists(ListNode[] lists) {
            ListNode head = null, tail = null;
            //建立一个优先级队列
            PriorityQueue<ListNode> heap = new PriorityQueue<>((n1, n2)->{
                return n1.val - n2.val;
            });
            int num = lists.length;   
            //依次放入每一个非空链表
            for(int i = 0; i < num; i++){
                if(lists[i] != null){
                    heap.offer(lists[i]);
                }
            }
            ListNode cur;
            while(!heap.isEmpty()){
                cur = heap.poll();
                if(cur.next != null){
                    heap.offer(cur.next);
                }
                if(head == null){
                    head = cur;                
                }
                else{
                    tail.next = cur;
                }
                tail = cur;            
            }
            if(tail != null){
                tail.next = null;
            }        
            return head;
        }
    }
    
  • 相关阅读:
    小程序组件事件
    关于微信小程序中组件和页面对全局样式的继承性
    创建Java程序并设置快捷提示
    微信小程序设置全局字体
    微信小程序组件的使用
    微信开发者工具的一些快捷键
    消除flex-wrap之后每个item上下的距离
    flex布局中的主轴和侧轴的确定
    C++string中用于查找的find系列函数浅析
    C++11 图说VS2013下的引用叠加规则和模板参数类型推导规则
  • 原文地址:https://www.cnblogs.com/echie/p/9573882.html
Copyright © 2020-2023  润新知