leetcode — merge-k-sorted-lists
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的更多相关文章
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- LeetCode:Merge k Sorted Lists
题目链接 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...
- LeetCode——Merge k Sorted Lists
Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...
- leetcode -- Merge k Sorted Lists add code
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [ ...
- LeetCode Merge k Sorted Lists (链表)
题意 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- [Leetcode] Merge k sorted lists 合并k个已排序的链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
- Leetcode:Merge k Sorted Lists分析和实现
题目大意是传入一个链表数组lists,每个链表都由若干个链接的链表结点组成,并且每个链表结点记录一个整数.题目保证传入的链表中的整数按从小到大进行排序. 题目要求我们输出一个新的链表,这个链表中应该包 ...
- LeetCode Merge k Sorted Lists 解决报告
https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...
- LeetCode —— Merge k Sorted Lists
/* ** 算法的思路: ** 1.将k个链表的首元素进行建堆 ** 2.从堆中取出最小的元素,放到链表中 ** 3.如果取出元素的有后续的元素,则放入堆中,若没有则转步骤2,直到堆为空 */ #in ...
随机推荐
- NPOI 导入为table 处理excel 格式问题
ICell cell = row.GetCell(j); if (!cell.isDbNullOrNull()) { switch (cell.CellType) { case CellType.Bl ...
- if else if else 语句
适合在程序中,实现多条件的判断 编写格式: if(条件){ if 执行体 }else if(条件){ if 执行体 }else if(条件){ if 执行体 }else{ else的执行体 } 当if ...
- Spring事务传递
2018-09-25 @Transactional(propagation=Propagation.NEVER) public void update(){ Session s = sessionFa ...
- java多线程系列7 高级同步工具(1)信号量Semaphore
Semaphore叫做信号量 可以控制某个资源可被同时访问的个数, acquire() 获取一个许可,得到许可才能执行后面的代码,如果没有就等待. release() 释放一个许可. 当信号量的只允许 ...
- springMVC学习 十二 拦截器
一 拦截器概述 拦截器技术比较像java web技术中的过滤器技术,都是发送 请求时被拦截器拦截,在控制器的前后添加额外功能.但是和Spring中的Aop技术是由区别的.AOP 在特定方法前后扩充(一 ...
- 【转】Map 与 Unordered_map
map和unordered_map的差别和使用 map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/b ...
- [solution] JZOJ-5458 质数
[solution] JZOJ-5458 质数 题面 Description 小X 是一位热爱数学的男孩子,在茫茫的数字中,他对质数更有一种独特的情感.小X 认为,质数是一切自然数起源的地方. 在小X ...
- The First BoKe
A.如何看待师生关系 说起师生关系,我们每个人都有不同的见解,但无一例外,师者,传道授业解惑也,老师的为学生传授的才能知识,是学生今后发展的宝贵财富,无论是从哪方面,都可以对学生起着积极向上的作用.而 ...
- RabbitMQ 常用操作
RabbitMQ简介 1.首先安装erlang rpm -Uvh https://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos ...
- 11.webview、shareSDK
布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:la ...