zoukankan      html  css  js  c++  java
  • 23. Merge k Sorted Lists

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

    本题目我第一个想到了用小顶堆来做,不是很难,直接上代码:

    /**

     * 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;

            PriorityQueue<ListNode> q = new PriorityQueue<ListNode>(lists.length,new Comparator<ListNode>(){

                public int compare(ListNode a,ListNode b){

                    return a.val-b.val;

                }

            });

            for(ListNode l:lists){

                if(l!=null) q.offer(l);

            }

            ListNode node = new ListNode(0);

            ListNode dummy = node;

            while(!q.isEmpty()){

                ListNode next = q.poll();

                node.next = next;

                node = node.next;

                next = next.next;

                if(next!=null) q.offer(next);

            }

            return dummy.next;

        }

    }

    后来看了标签,发现还可以用分治的方法来做(也就是递归),代码如下:

    /**

     * 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.length==0) return null;

            if(lists.length==1) return lists[0];

            if(lists.length==2) return mergeTwoLists(lists[0],lists[1]);

            return mergeTwoLists(mergeKLists(Arrays.copyOfRange(lists,0,lists.length/2)),mergeKLists(Arrays.copyOfRange(lists,lists.length/2,lists.length)));

        }

        public ListNode mergeTwoLists(ListNode l1,ListNode l2){

            ListNode node = new ListNode(0);

            ListNode dummy =node;

            while(l1!=null&&l2!=null){

                if(l1.val<l2.val){

                    ListNode next = l1;

                    node.next = next;

                    node = node.next;

                    l1 = l1.next;

                }else{

                    ListNode next = l2;

                    node.next = next;

                    node = node.next;

                    l2 = l2.next;

                }

            }

            if(l1!=null){

                node.next = l1;

            }

            if(l2!=null){

                node.next = l2;

            }

            return dummy.next;

        }

    }

  • 相关阅读:
    前端方便面
    在页面未加载完之前显示loading动画
    块级格式化上下文(BFC)
    css预编译--sass进阶篇
    IPhoneX网页布局简介
    kotlin回调函数作为参数block: T.() -> Unit和block: () -> Unit的区别
    flutter显示参数提示的快捷键
    LinuxC线程pthread线程同步进程同步-互斥量、信号量、条件变量、读写锁、文件锁
    flutter实现页面跳转的两种路由
    android开发FontMetrics的理解
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6359936.html
Copyright © 2011-2022 走看看