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. Pandas 合并 concat

    pandas处理多组数据的时候往往会要用到数据的合并处理,使用 concat是一种基本的合并方式.而且concat中有很多参数可以调整,合并成你想要的数据形式. 1.axis(合并方向):axis=0 ...

  2. 【Spark2.0源码学习】-8.SparkContext与Application介绍

             在前面的内容,我们针对于RpcEndpoint启动以及RpcEndpoint消息处理机制进行了详细的介绍,在我们的大脑里,基本上可以构建Spark各节点的模样.接下来的章节将会从Sp ...

  3. Linux 使用 mail 发送邮件

    ubuntu 需要安装 mailutils sudo apt-get install mailutils

  4. android 获取文本框回车输入

    扫描头开启,并发送回车 txtUsername.setOnEditorActionListener(new OnEditorActionListener() { @Override public bo ...

  5. Docker 启动不了容器的问题

    今天在运行 docker 的时候,就是执行 docker exec 命令的时候,发现一直报错.具体的报错信息如下: Error response from daemon: Container XXX ...

  6. java基础知识-方法

    1.方法 定义:一段定义在类中的业务逻辑的代码. 目的:封装右业务关系的代码,实现代码的复用,即简化代码书写. 2.方法定义的格式 修饰符,返回值类型 方法名(数据类型1,形参名1,数据类型2,形参2 ...

  7. 初识Twisted(一)

    pip install Twisted 安装Twisted库 from twisted.internet import reactor #开启事件循环 #不是简单的循环 #不会带来任何性能损失 rea ...

  8. Android-Java-静态变量

    描述Person对象: package android.java.oop09; // 描述Person对象 public class Person { private String name; pri ...

  9. 在Azure DevOps Server (TFS 2019) 流水线传递参数

    变量概述 在Azure DevOps Server的流水线中,变量是衔接不同任务和不通代理之间的桥梁,它可以使相对松散.各自独立的任务之间相关影响并共享数据.在流水线中使用变量,可以在各任务之间相互调 ...

  10. mac下安装安卓开发环境

    对于做ios的人来说,安装安卓开发环境,最好是在mac下安装了,我的mac是10.8.2,64位系统的 安卓开发环境需要下面几个东西: 1 jdk(mac下已经默认有了,可以在命令提示符下输入java ...