Merge k Sorted Lists

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

 
采用优先队列priority_queue
把ListNode放入优先队列中,弹出最小指后,如果该ListNode有下一个元素,则把下一个元素放入到队列中
 
 
 /**
* 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]!=NULL) q.push(lists[i]);
} ListNode *front=new ListNode();
ListNode *result;
result=front; while(!q.empty())
{
ListNode *tmp=q.top();
front->next=tmp;
if(tmp->next!=NULL) q.push(tmp->next);
q.pop();
front=tmp;
} front->next=NULL; ListNode *tmp=result;
result=result->next;
delete tmp;
return result; }
};

【leetcode】Merge k Sorted Lists的更多相关文章

  1. 【leetcode】Merge k Sorted Lists(按大小顺序连接k个链表)

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

  2. 【Leetcode】【Hard】Merge k Sorted Lists

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

  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】Merge Two Sorted Lists(easy)

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

  5. 【LeeCode23】Merge k Sorted Lists★★★

    1.题目描述: 2.解题思路: 题意:将K个已经排序的链表合并成一个排序的链表,分析并描述所用算法的复杂度. 方法一:基于“二分”思想的归并排序.本文用非递归和递归两种方法实现. (1)非递归:归并排 ...

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

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

  7. 【LeetCode练习题】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  8. Java for LeetCode 023 Merge k Sorted Lists

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

  9. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

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

随机推荐

  1. mysql dumpfile与outfile函数的区别

    一直以为两个函数作用是相同的   经过简单测试发现还是有些区别的   如下表admin   mysql> select * from admin;   +-----+-----------+-- ...

  2. array_map与array_column之间的关系

    /*|----------------------------------------------------------|array_map();将回调函数作用到给定数组的单元上|array_col ...

  3. jQuery.imgLazyLoad图片懒加载组件

    一.前言 当一个页面中请求的图片过多,而且图片太大,页面访问的速度是非常慢的,对用户的体验非常不友好:使用图片懒加载,可以减轻服务器的压力,增加页面的访问量,这里主要是总结一下我自己写的图片懒加载组件 ...

  4. Linq→join中指定多个条件

    还是习惯先撸一段SQL * FROM User_Pic P AND P.Guid = R.UserPicGuid ORDER BY PicSize DESC 然后发现Linq中的join不能多条件.. ...

  5. Swift编程语言资料合集

    在本周二凌晨召开的苹果年度开发者大会WWDC上,苹果公司推出了全新的编程语言Swift.Swift 基于C和Objective-C,是供iOS和OS X应用编程的全新语言,更加高效.现代.安全,可以提 ...

  6. eclipse tomcat debug启动慢

    myeclipse或eclipse下debug模式启动很慢,默认模式也是debug,网上找了终于解决, 原因是有eclipse或myeclipse启动debug时自动添加断点,所以必须删除一些东西. ...

  7. cf#306D. Regular Bridge(图论,构图)

    D. Regular Bridge time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. Hdu.1325.Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  9. Mac Pro 安装 cmake,报错 Warning: cmake-3.5.2 already installed, it's just not linked

    1.先安装 brew,参考文章:Mac Pro 安装 Homebrew 软件包管理工具 2.执行安装命令 brew install cmake 出现警告提示: Warning: cmake-3.5.2 ...

  10. EasyUI中datagrid控件的使用 设置多行表头(两行或多行)

    EasyUI中的datagrid控件十分强大,能生成各种复杂的报表,现在因为项目需要,需要生成一个表头两行的表,找了一些说明文档,以下用一个实例来说明一下: 第一种方法: $('#divData'). ...