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. Python基本数据类型集合、格式化、函数

    一.变量总结 1.1 变量定义 记录某种状态或者数值,并用某个名称代表这个数值或状态. 1.2 变量在内存中的表现形式 Python 中一切皆为对象,数字是对象,列表是对象,函数也是对象,任何东西都是 ...

  2. Python3之os模块

    一:简介 os模块主要用于提供系统高级别的操作. 二:常用方法 os.access(path, mode) # 检验权限模式 os.chdir(path) # 改变当前工作目录 os.chflags( ...

  3. loj #2538. 「PKUWC2018」Slay the Spire

    $ \color{#0066ff}{ 题目描述 }$ 九条可怜在玩一个很好玩的策略游戏:Slay the Spire,一开始九条可怜的卡组里有 \(2n\) 张牌,每张牌上都写着一个数字\(w_i\) ...

  4. 设置placeholder的样式

    :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #f00; } ::-moz-placeholder { /* Mozilla Fir ...

  5. 【问题记录】element is not attached to the page document

    遇到ui脚本报错:element is not attached to the page document 解决办法,再次定位即可

  6. 导出table为Excel

    1.HTML <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=" ...

  7. 洛谷 P1217 [USACO1.5]回文质数 Prime Palindrome

    嗯... 这道题对于蒟蒻的我来说实在是TQL... 先看一下题:(题目链接:https://www.luogu.org/problemnew/show/P1217) 然后说一下我的做题过程吧: 一看到 ...

  8. 老实pear_Excel 操作类 Spreadsheet_Excel_Writer 常用参数说明

    (如果是PHP5项目就不用往下看了,因为PHP5项目可以直接用PHPExcel,方便快捷) 手上有个PHP4的修改项目,要修改Excel的导出,然后再把导出的Excel再导入到系统里. 在导入的时候, ...

  9. Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement

    原因: 此异常的原因是由于mapper接口编译后在同一个目录下没有找到mapper映射文件而出现的.由于maven工程在默认情况下src/main/java目录下的mapper文件是不发布到targe ...

  10. 蓝桥-青蛙跳杯子(bfs)

    问题描述 X星球的流行宠物是青蛙,一般有两种颜色:白色和黑色. X星球的居民喜欢把它们放在一排茶杯里,这样可以观察它们跳来跳去. 如下图,有一排杯子,左边的一个是空着的,右边的杯子,每个里边有一只青蛙 ...