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. 32.Mysql Cluster

    32.Mysql Cluster Cluster是一组节点的组合.节点分为数据节点.SQL节点.管理节点.节点组合在一起可以为应用提供高可用.高性能.可缩放的Cluster数据管理.数据节点使用NDB ...

  2. concurrent.futures模块(进程池/线程池)

    需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...

  3. C++学习札记(1)

    指针 按别名传递 下面是个例子: #include <iostream> using namespace std; void swap(int &a,int &b) { i ...

  4. linux虚拟机配置上网(静态IP)和配置tomcat服务环境

    常用命令:vi或者vim编辑 ,按i编辑模式,按ecs进入基本模式,按 :w  保存:按 :wq  退出并保存:mv移动::q退出 :ln -sv apache-tomcat-8.0.24 tomca ...

  5. node.js获取参数的常用方法

    1.req.body 2.req.query 3.req.params 一.req.body例子 body不是nodejs默认提供的,你需要载入body-parser中间件才可以使用req.body, ...

  6. 32 bit 与 64 bit 程序(1)如何识别?

    一, 怎样判断一个exe可执行程序是32位的还是64位的? 简单的方法: 一般来说在64位的windows7下,打开任务管理器可以知道哪些程序是32位的哪些是64位的,但是因为自己的电脑是64位的wi ...

  7. PHPCMS9.6.0最新版SQL注入和前台GETSHELL漏洞分析 (实验新课)

    PHPCMS9.6.0最新版中,由于/modules/attachment/attachments.php的过滤函数的缺陷导致了可以绕过它的过滤机制形成SQL注入漏洞,可导致数据库中数据泄漏. 而且在 ...

  8. Navicat Premium Mac V12.0.22.0 中英文破解 亲测可用

    换了Mac电脑后网上找了好些个 Navicat Premium 破解版本, 特别是CSDN上要积分下载的也不能用,浪费积分下,都是些坑爹破解方法,浪费了好些时间,今天介绍看到的一套有效的破解过程 1. ...

  9. web API简介(二):客户端储存之document.cookie API

    概述 前篇:web API简介(一):API,Ajax和Fetch 客户端储存从某一方面来说和动态网站差不多.动态网站是用服务端来储存数据,而客户端储存是用客户端来储存数据.document.cook ...

  10. struts2框架学习笔记4:获取参数

    第一种参数获取方式: 编写一个前端页面,提交表单,做示例: <form action="${pageContext.request.contextPath}/Demo1Action&q ...