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: 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. 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. 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. [Leetcode][Python]23: Merge k Sorted Lists

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

  8. LeetCode之“链表”:Merge Two Sorted Lists && 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. 方案:解决 wordpress 中 gravatar 头像被墙问题

    Gravatar头像具有很好的通用性,但是却遭到了无辜的拦截,对于无法加载头像URL,我们在WordPress系统中通过修改默认的URL链接可以达到恢复头像的功能. 修改文件路径为 /wp-inclu ...

  2. bwlabel函数的c++实现

    实验中需要用到区域联通的算法,就是类似于matlab中bwlabel的函数.网上找了找c++源码未果,bwlabel-python版用python描述了matlab中的实现方法,但是最后对标签的处理部 ...

  3. 基础总结篇之四:Service完全解析

    富貴必從勤苦得,男兒須讀五車書.唐.杜甫<柏學士茅屋> 作为程序员的我们,须知富贵是要通过勤苦努力才能得到的,要想在行业内有所建树,就必须刻苦学习和钻研. 今天我们来讲一下Android中 ...

  4. Highcharts 非常实用的Javascript统计图

    Highcharts 官网: http://www.highcharts.com Highcharts 官网示例:http://www.highcharts.com/demo/ Highcharts ...

  5. java与.net比较学习系列(2) 基础语言要素

    这一篇从最基础的开始对比总结,说起基础语言要素,故名思义,就是学习语言的基础,主要内容包括标识符,关键字和注释.我想从以下几点进行总结,有区别的地方有都使用红色粗体字进行了总结. 1,标识符 2,关键 ...

  6. [Elasticsearch] 部分匹配 (三) - 查询期间的即时搜索

    本章翻译自Elasticsearch官方指南的Partial Matching一章. 查询期间的即时搜索(Query-time Search-as-you-type) 如今让我们来看看前缀匹配可以怎样 ...

  7. Sprite Kit编程指南(1)-深入Sprite Kit

    深入Sprite Kit 学习Sprite Kit最好的方法是在实践中观察它.此示例创建一对场景和各自的动画内容.通过这个例子,你将学习使用Sprite Kit内容的一些基础技术,包括: ·      ...

  8. 总结oninput、onchange与onpropertychange事件的用法和区别 书写搜索的神奇代码

    总结oninput.onchange与onpropertychange事件的用法和区别 最近手机开发一个模糊搜索的功能组建,在网上就找到这篇文章! 前端页面开发的很多情况下都需要实时监听文本框输入,比 ...

  9. asp.net向后台传参数动态加载图片

    //向后台传参数动态加载图片 $(function() { $("#Button1").click(function() { var stockcode = getUrlParam ...

  10. Android-------手机屏幕适配之文件适配

    public class Main {         //定义文件本地存储路径,可按照需求更改         private final static String rootPath = &quo ...