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

Example:

Input:
[
  1->4->5,
  1->3->4,
  2->6
]
Output: 1->1->2->3->4->4->5->6

要合并K个排好序的链表,我用的方法是用一个优先队列每次存K个元素在队列中,根据优先队列的属性把小的数放在最前面,这样每次
取出队首就是我们要的,这样把K个链表全部过一遍优先队列就得到了排好序的新序列,不了解优先队列的可以看这里http://www.importnew.com/6932.html
(这里比较奇怪的是我自己的从测试代码是没问题的,第一个测试样例过了但是提交却显示)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
Comparator<ListNode> idComparator=new Comparator<ListNode>() {
@Override
public int compare(ListNode l1,ListNode l2) {
return l1.val-l2.val;
}
};
int len=lists.length;
Queue<ListNode> q=new PriorityQueue<ListNode>(len,idComparator);
for(int i=0;i<len;i++) {
if(lists[i]!=null) q.add(lists[i]);
}
ListNode res=new ListNode(0), head=res,temp=null;
while(!q.isEmpty()) {
temp=q.poll();
head.next=temp;
head=head.next;
if(temp.next!=null) q.add(temp.next);
}
return res.next;
}
}

下面是我的测试代码

public class Test {        

    public static void main(String[] args) {
Random rand=new Random();
int max=0;
ListNode t1=new ListNode(-1);
ListNode l1=t1;
int a=0;
for(int i=0;i<5;i++) {
a=rand.nextInt(100);
if(a>max) max=a;
t1.next=new ListNode(max);
t1=t1.next;
} ListNode t2=new ListNode(-1);
ListNode l2=t2;
int b=0;
max=b;
for(int i=0;i<7;i++) {
b=rand.nextInt(100);
if(b>max) max=b;
t2.next=new ListNode(max);
t2=t2.next;
} ListNode t3=new ListNode(-1);
ListNode l3=t3;
int c=0;
max=c;
for(int i=0;i<3;i++) {
c=rand.nextInt(100);
if(c>max) max=c;
t3.next=new ListNode(max);
t3=t3.next;
} ListNode[] lists= {l1.next,l2.next,l3.next};
ListNode temp=new ListNode(-1); Solution s=new Solution();
temp=s.mergeKLists(lists); while(temp!=null) {
System.out.print(temp.val+"--->");
temp=temp.next;
}
}
} class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
Comparator<ListNode> idComparator=new Comparator<ListNode>() {
@Override
public int compare(ListNode l1,ListNode l2) {
return l1.val-l2.val;
}
};
int len=lists.length;
Queue<ListNode> q=new PriorityQueue<ListNode>(len,idComparator);
for(int i=0;i<len;i++) {
if(lists[i]!=null) q.add(lists[i]);
}
ListNode res=new ListNode(0), head=res,temp=null;
while(!q.isEmpty()) {
temp=q.poll();
head.next=temp;
head=head.next;
if(temp.next!=null) q.add(temp.next);
}
return res.next;
}
}

我自己测试是没有问题的,但是提交不过也没弄明白

下面是c++版本的代码

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
struct cmp {
bool operator () (ListNode *a, ListNode *b) {
return a->val > b->val;
}
}; class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists) {
priority_queue<ListNode*, vector<ListNode*>, cmp> q;
for (int i = ; i < lists.size(); ++i) {
if (lists[i]) q.push(lists[i]);
}
ListNode *head = NULL, *pre = NULL, *tmp = NULL;
while (!q.empty()) {
tmp = q.top();
q.pop();
if (!pre) head = tmp;
else pre->next = tmp;
pre = tmp;
if (tmp->next) q.push(tmp->next);
}
return head;
}
};

优先队列的底层是通过的小根堆实现的,通过compare把小的元素放在前面,取出队首后维护这个小根堆,元素加入堆中的复杂度为O(longk),总共有kn个元素加入堆中,因此,是O(nklogk)

[LeetCode]23. Merge k Sorted Lists合并K个排序链表的更多相关文章

  1. [LeetCode]21. Merge Two Sorted Lists合并两个有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  2. LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  3. 【LeetCode每天一题】Merge Two Sorted Lists(合并两个排序链表)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  4. leetcode 21 Merge Two Sorted Lists 合并两个有序链表

    描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...

  5. 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

  6. [LeetCode] 21. Merge Two Sorted Lists 合并有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  7. [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

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

  8. 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...

  9. [LeetCode] Merge k Sorted Lists 合并k个有序链表

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

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

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

随机推荐

  1. The server of Apache (二)——apache服务客户端验证

    一.确定网站名称.IP地址 地址为: 192.168.1.1   域名为: www.benet.com 二.配置可用的DNS域名服务或者修改本地hosts记录 ~] # vim /etc/hosts ...

  2. 跟我一起读postgresql源码(五)——Planer(查询规划模块)(下)

    上一篇我们介绍了查询规划模块的总体流程和预处理部分的源码.查询规划模块再执行完预处理之后,可以进入正式的查询规划处理流程了. 查询规划的主要工作由grouping_planner函数完成.在具体实现的 ...

  3. Flink学习笔记:Operators之Process Function

    本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKhaz ...

  4. c语言数据结构学习心得——线性表

    线性表:具有相同数据类型的n(n>0)个数据元素的有限序列. 主要有顺序存储和链式存储. 顺序存储: 特点:地址连续,随机/存取,顺序存储. 建立:首地址/存储空间大小(数组),表长. 方式:静 ...

  5. webpack(二)解析es6并打包

    一.前言 ECMAScript 6(ES6)的发展速度非常之快,但现代浏览器对ES6新特性支持度不高,所以要想在浏览器中直接使用ES6的新特性就得借助别的工具来实现.Babel是一个广泛使用的转码器, ...

  6. ubuntu 上的python不能解析jpeg,png?

    在安装pillow之前需要先安装以下支持包1.apt-get install libjpeg-dev libfreetype6-dev zlib1g-dev libpng12-dev2.安装pillo ...

  7. STM32F0 中 ADC 多通道转换结果相同的问题

    前言 前段时间调试 STM32F030 的 ADC,在多通道转换时遇到了奇怪的问题,使用官方的例程和库函数连续转换多个 ADC 通道,得到的几个通道的结果是一样的,解决办法参考了 关于STM32F0系 ...

  8. 【数学】【筛素数】Miller-Rabin素性测试 学习笔记

        Miller-Rabin是一种高效的随机算法,用来检测一个数$p$是否是素数,最坏时间复杂度为$\log^3 p$,正确率约为$1-4^{-k}$,$k$是检验次数. 一.来源     Mil ...

  9. bzoj1041 圆上的整点 数学

    题目传送门 题目大意:求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数. 思路:没思路,看大佬的博客(转载自https://blog.csdn.net/csyzcyj),转载只 ...

  10. OJ 21651::Cow Hurdles(佛罗一德的变式)

    Description Farmer John wants the cows to prepare for the county jumping competition, so Bessie and ...