Merge k Sorted Lists 解答

时间:2022-01-07 04:58:13

Question

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

Solution 1 -- Merge Sort

We can follow the method of merge sort. Time complexity O(nlog(n)), n is the total number of all nodes.

 /**
* 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 < 1)
return null;
if (lists.length == 1)
return lists[0];
int k = lists.length;
return MSort(lists, 0, k - 1);
} private ListNode MSort(ListNode[] lists, int start, int end) {
if (start < end) {
int mid = (end - start) / 2 + start;
ListNode left = MSort(lists, start, mid);
ListNode right = MSort(lists, mid + 1, end);
return mergeTwoLists(left, right);
}
return lists[start];
} private ListNode mergeTwoLists(ListNode a, ListNode b) {
ListNode newHead = new ListNode(0);
ListNode p = newHead, p1 = a, p2 = b;
while (p1 != null && p2 != null) {
if (p1.val < p2.val) {
p.next = new ListNode(p1.val);
p1 = p1.next;
} else {
p.next = new ListNode(p2.val);
p2 = p2.next;
}
p = p.next;
}
while (p1 != null) {
p.next = new ListNode(p1.val);
p1 = p1.next;
p = p.next;
}
while (p2 != null) {
p.next = new ListNode(p2.val);
p2 = p2.next;
p = p.next;
}
return newHead.next;
}
}

Solution 2 -- PriorityQueue

We can use PriorityQueue in Java. The elements of the priority queue are ordered according to their natural ordering, or by a comparator provided at the construction time.

PriorityQueue

Comparator

 /**
* 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 < 1)
return null;
ListNode newHead = new ListNode(0);
PriorityQueue<ListNode> pq = new PriorityQueue<ListNode>(lists.length,
new Comparator<ListNode>() {
public int compare(ListNode a, ListNode b) {
return (a.val - b.val);
}});
for (ListNode tmp : lists) {
if (tmp != null)
pq.add(tmp);
}
ListNode p = newHead;
while (pq.size() > 0) {
ListNode tmp = pq.poll();
p.next = tmp;
if (tmp.next != null)
pq.add(tmp.next);
p = p.next;
}
return newHead.next;
}
}

Merge k Sorted Lists 解答的更多相关文章

  1. LeetCode&colon; Merge k Sorted Lists 解题报告

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  2. 刷题23&period; Merge k Sorted Lists

    一.题目说明 这个题目是23. Merge k Sorted Lists,归并k个有序列表生成一个列表.难度为Hard,实际上并不难,我一次提交就对了. 二.我的解答 就是k路归并,思路很简单,实现也 ...

  3. Merge k Sorted Lists

    1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...

  4. 71&period; Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  5. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  6. 【LeetCode练习题】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  7. &lbrack;Leetcode&rsqb;&lbrack;Python&rsqb;23&colon; Merge k Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...

  8. LeetCode之&OpenCurlyDoubleQuote;链表”:Merge Two Sorted Lists &amp&semi;&amp&semi; Merge k Sorted Lists

    1. Merge Two Sorted Lists 题目链接 题目要求:  Merge two sorted linked lists and return it as a new list. The ...

  9. leetcode-algorithms-23 Merge k Sorted Lists

    leetcode-algorithms-23 Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted ...

随机推荐

  1. C&num; 多线程同步和线程通信

    多线程通信 1. 当线程之间有先后的依赖关系时,属于线程之间的通信问题.也就是后一个线程要等待别的一个或多个线程全部完成,才能开始下一步的工作.可以使用: WaitHandle Class WaitH ...

  2. Linux启动新进程的几种方法及比较

    有时候,我们需要在自己的程序(进程)中启动另一个程序(进程)来帮助我们完成一些工作,那么我们需要怎么才能在自己的进程中启动其他的进程呢?在Linux中提供了不少的方法来实现这一点,下面就来介绍一个这些 ...

  3. docker运行dubbo-admin

    一:简介 dubbo-admin是dubbo框架的管理平台. 二: 创建继续镜像 Dockerfile FROM fangjipu/jdk8:8 RUN yum -y install epel-rel ...

  4. 【Android学习笔记】布局的简单介绍

    我在学习Android开发的时候是基于实战项目的,基础理论知识以前也是零散的看过一些,个人还是觉得边做项目边学要快些.现在做的这个项目iOS端是我做的,这样逻辑什么的都很熟悉,于我而言换个平台也只是换 ...

  5. How to create DB2 user function easily by DB Query Analyzer 6&period;03

    How to create DB2user function easily by DB Query Analyzer 6.03 Ma Genfeng (Guangdong Unitoll Servic ...

  6. iOS开发基础-九宫格坐标&lpar;5&rpar;

    继续在iOS开发基础-九宫格坐标(4)的基础上进行优化. 一.改进思路 1)iOS开发基础-九宫格坐标(4)中 viewDidLoad 方法中的第21.22行对控件属性的设置能否拿到视图类 WJQAp ...

  7. 关于最新笔记本机型预装win8如何更换为win7的解决办法

    关于最新笔记本机型预装win8如何更换为win7的解决办法 目前新出的很多机型出厂自带的都是win8系统,可能有些人用不习惯,想更换为win7系统,但是由于这些机型主板都采用UEFI这种接口(硬盘分区 ...

  8. C&plus;&plus;中的清屏函数

    system("cls") 执行控制台命令cls,功能是清屏,清楚所有屏幕显示信息

  9. 32bit 天堂2 windows 2003 server架设教程

    安装环境::[注意:本教程newauth要用不加密的版本] windows 2003 enterprise server 100用户license Microsoft sql server 2000 ...

  10. Externalizable的使用方法

    package com.itbuluoge.object; import java.io.Externalizable; import java.io.FileInputStream; import ...