leetcode Merge k Sorted Lists
2015-12-11 20:58
274 查看
原题链接:https://leetcode.com/problems/merge-k-sorted-lists/
Description
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: struct cmp { inline bool operator()(ListNode *&x, ListNode *&y) { return x->val > y->val; } }; ListNode* mergeKLists(vector<ListNode*>& lists) { priority_queue<ListNode*, vector<ListNode*>, cmp> q; for (auto r : lists) { while (r) { q.push(r); r = r->next; } } ListNode root(0), *p = &root; while (!q.empty()) { p->next = q.top(); q.pop(); p = p->next; } p->next = NULL; return root.next; } };
相关文章推荐
- [Leetcode]Range Sum Query 2D - Immutable
- 接口开发
- BFS
- Tomcat使用IDEA远程Debug调试
- 我的CS学习路线图
- 贝叶斯决策
- 在Allegro中放置的元件显示不出来E- (SPMHA1-150): Symbol does not fit into the drawing ... reposition the symbol.
- libevent在windows下使用步骤详解
- Linux文件解压
- Google I/O 2014? No,Android I/O 2014
- C#理解AutoResetEvent和ManualResetEvent
- OC Foundation框架—字符串操作方法及习题
- leetcode Remove Element
- nginx忽略favicon.ico日志
- ThinkPHP--改变状态--某个键值的来回切换操作
- HDU 2236 无题II 二分+最大匹配
- PAT乙级——1012 D进制的A+B (C/C++)
- mysql 分表查询
- 关于WebView顶部的滚动条简单但记录下供以后使用
- C++ vector