leetcode - Merge k Sorted Lists
2014-10-25 18:06
211 查看
Merge k sorted
linked lists and return it as one sorted list. Analyze and describe its complexity.
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) {} * }; */ struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution { struct CompareNode { bool operator()(ListNode* const & p1, ListNode* const & p2) { return p1->val > p2->val; } }; public: ListNode *mergeKLists(std::vector<ListNode *> &lists) { ListNode dummy(0); ListNode* tail=&dummy; std::priority_queue<ListNode*,std::vector<ListNode*>,CompareNode> queue; for (std::vector<ListNode *>::iterator it = lists.begin(); it != lists.end(); ++it) { if (*it) queue.push(*it); } while (!queue.empty()) { tail->next=queue.top(); queue.pop(); tail=tail->next; if (tail->next) { queue.push(tail->next); } } return dummy.next; } };
相关文章推荐
- [LeetCode] Merge k Sorted Lists
- [LeetCode][JavaScript]Merge k Sorted Lists
- LeetCode Merge k Sorted Lists
- Leetcode: Merge k Sorted Lists
- LeetCode 21 - Merge Two Sorted Lists
- leetcode---merge-two-sorted-lists---链表
- 【leetcode】【单链表】【61】Merge k Sorted Lists
- leetcode笔记:Merge Two Sorted Lists
- LeetCode_Easy心得:21. Merge Two Sorted Lists(C语言)
- [Leetcode] Merge k Sorted Lists
- leetcode---merge-k-sorted-lists---链表
- 个人记录-LeetCode 23. Merge k Sorted Lists
- 2017-09-11 LeetCode_021 Merge Two Sorted Lists
- 【leetcode】Merge k Sorted Lists(按大小顺序连接k个链表)
- Merge k Sorted Lists leetcode java
- Leetcode-Merge Two Sorted Lists
- leetcode 23. Merge k Sorted Lists
- Leetcode Merge k Sorted Lists
- leetcode: (21) Merge Two Sorted Lists
- LeetCode Merge Two Sorted Lists