import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue; /**
* Source : https://oj.leetcode.com/problems/merge-k-sorted-lists/
*
* Created by lverpeng on 2017/7/11.
*
* Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
*
*/
public class MergeKList { /**
* 两两归并排序
*
* @param headList
* @return
*/
public Node merge (List<Node> headList) {
while (headList.size() > 1) {
Node list1 = headList.remove(headList.size() - 1);
Node list2 = headList.remove(headList.size() - 1);
Node mergeList = mergeTwoList(list1, list2);
headList.add(0, mergeList);
}
return headList.get(0); } /**
* 对k个链表做归并排序,比较k个元素的时候使用最小堆排序
*
* @param headList
* @return
*/
public Node merge0 (List<Node> headList) {
PriorityQueue<Node> priorityQueue = new PriorityQueue<Node>();
for (Node node : headList) {
priorityQueue.add(node);
} Node head = null;
Node current = null;
while (priorityQueue.size() > 0) {
Node node = priorityQueue.poll();
if (head == null) {
head = node;
current = node;
} else {
if (current == node) {
throw new IllegalArgumentException("出现了循环引用,可能是多个链表中有相同的元素");
}
current.next = node;
current = current.next;
}
if (node.next != null) {
priorityQueue.add(node.next);
}
}
return head;
} /**
* 归并排序
*
* @param list1
* @param list2
* @return
*/
public static Node mergeTwoList (Node list1, Node list2) {
Node head = null;
Node current = null;
while (list1 != null && list2 != null) {
Node node = null;
if (list1.value > list2.value) {
node = list2;
list2 = list2.next;
} else {
node = list1;
list1 = list1.next;
}
if (head == null) {
head = current = node;
} else {
current.next = node;
current = current.next;
}
} list1 = list1 == null ? list2 : list1;
if (list1 != null) {
if (head != null) {
current.next = list1;
} else {
head = list1;
}
}
return head;
} private static class Node implements Comparable<Node>{
int value;
Node next; @Override
public String toString() {
return "Node{" +
"value=" + value +
", next=" + (next == null ? "" : next.value) +
'}';
} @Override
public int compareTo(Node o) {
return this.value - o.value;
}
} private static void print (Node node) {
while (node != null) {
System.out.println(node);
node = node.next;
}
} public static void main(String[] args) {
Node list1 = new Node();
Node pointer1 = list1;
list1.value = 1;
Node list2 = new Node();
list2.value = 2;
Node pointer2 = list2;
Node list3 = new Node();
list3.value = 3;
Node pointer3 = list3;
for (int i = 4; i < 20; i++) {
Node node = new Node();
node.value = i;
if (i % 3 == 1) {
pointer1.next = node;
pointer1 = pointer1.next;
} else if (i % 3 == 2) {
pointer2.next = node;
pointer2 = pointer2.next;
} else {
pointer3.next = node;
pointer3 = pointer3.next;
}
} List<Node> list = new ArrayList<Node>();
list.add(list1);
list.add(list1);
list.add(list1);
MergeKList mergeKList = new MergeKList();
// Node result = mergeKList.merge(list);
// print(result); System.out.println(); Node result = mergeKList.merge0(list);
print(result); }
}

leetcode — 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. [LeetCode] Merge k Sorted Lists 合并k个有序链表

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

  3. LeetCode:Merge k Sorted Lists

    题目链接 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...

  4. LeetCode——Merge k Sorted Lists

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

  5. leetcode -- Merge k Sorted Lists add code

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

  6. LeetCode Merge k Sorted Lists (链表)

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

  7. [Leetcode] Merge k sorted lists 合并k个已排序的链表

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

  8. Leetcode:Merge k Sorted Lists分析和实现

    题目大意是传入一个链表数组lists,每个链表都由若干个链接的链表结点组成,并且每个链表结点记录一个整数.题目保证传入的链表中的整数按从小到大进行排序. 题目要求我们输出一个新的链表,这个链表中应该包 ...

  9. LeetCode Merge k Sorted Lists 解决报告

    https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...

  10. LeetCode —— Merge k Sorted Lists

    /* ** 算法的思路: ** 1.将k个链表的首元素进行建堆 ** 2.从堆中取出最小的元素,放到链表中 ** 3.如果取出元素的有后续的元素,则放入堆中,若没有则转步骤2,直到堆为空 */ #in ...

随机推荐

  1. 给 Chrome浏览器 添加 Javascript小书签,查看当前页面全部加载的javascript文件及代码片段

    小书签又名 Bookmarklet,由英文单词 Bookmark 和 Applet 组合而来.简单地说,小书签就是把一段带有特定功能的 JavaScript 代码保存至收藏夹,当你需要的时候点击它来实 ...

  2. FastFDS安装及简单使用

    1. FastDFS安装(ubuntu) 需要准备: nginx.fastdfs.libfastcommon.gcc.git apt-get update apt-get -y install mak ...

  3. HOSTNAME问题 和yum配置163源的操作 安装lsb_release,KSH,CSH

    HOSTNAME 在 /etc/hosts 里添加一行 127.0.0.1 yourhostname yum配置 来自http://www.cnblogs.com/wutengbiao/p/41889 ...

  4. C++ 提取网页内容系列之五 整合爬取豆瓣读书

    工作太忙 没有时间细化了 就说说 主要内容吧 下载和分析漫画是分开的 下载豆瓣漫画页面是使用之前的文章的代码 见http://www.cnblogs.com/itdef/p/4171179.html ...

  5. Acegi框架

    Acegi(Acegi Security)框架,是一个能为基于Spring的企业应用提供强大而灵活安全访问控制解决方案的框架,Acegi已经成为 Spring官方的一个子项目,所以也称为Spring ...

  6. 服务器安装ubuntu 14.04 server,开机启动屏幕不停滚动错误WRITE SAME failed. Manually zeroing

    昨天给服务器上安装了一个Ubuntu-14.04-server系统,安装完成后系统可以正常启动,但屏幕上一直滚动着一个错误,sda1:WRITE SAME failed. Manually zeroi ...

  7. toast

     start (e){          wx.showToast({         title: '轮播图',         icon: 'success',         duration: ...

  8. STM32F1xx寄存器版库

    文件下载链接 https://files.cnblogs.com/files/listenscience/STM32F1xx%28MDK5%292018.10.20.rar

  9. 【pycharm 警告】unittest RuntimeWarning: Parent module ” not found while handling absolute import

    Pycharm 2016.2执行单元测试遇到如下问题: RuntimeWarning: Parent module ‘YOUR_MODULE_HERE’ not found while handlin ...

  10. Oracle数据库查询基本数据

    ------------------------------------------------------------------找出EMP表select * from EMP;--选择在部门30中 ...