码迷,mamicode.com
首页 > 其他好文 > 详细

[Leetcode] Merge k Sorted Lists

时间:2014-06-10 19:51:30      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   code   java   

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

 

Solution: Find the smallest list-head first using minimum-heap(lgk).

 

complexity: O(NlgK)
 
参考代码:
 
https://github.com/AnnieKim/LeetCode/blob/master/MergekSortedLists.h
 
bubuko.com,布布扣
 1 class Mycompare {
 2 public:
 3     bool operator()(ListNode *a, ListNode *b) {
 4         return a->val > b->val;
 5     }
 6 };
 7 
 8 class Solution {
 9 public:
10     ListNode *mergeKLists(vector<ListNode *> &lists) {
11         priority_queue<ListNode *, vector<ListNode *>, Mycompare> q;
12         for (int i = 0; i < lists.size(); ++i)
13             if (lists[i])
14                 q.push(lists[i]);
15         
16         ListNode dummy(0), *cur = &dummy;
17         while (!q.empty()) {
18             ListNode *node = q.top();
19             q.pop();
20             cur = cur->next = node;
21             if (node->next)
22                 q.push(node->next);
23         }
24         return dummy.next;
25     }
26 };
bubuko.com,布布扣
 
https://github.com/mengli/leetcode/blob/master/MergekSortedLists.java
bubuko.com,布布扣
 1 public class MergekSortedLists {
 2     public ListNode mergeKLists(ArrayList<ListNode> lists) {
 3         if (lists == null || lists.isEmpty())
 4             return null;
 5 
 6         Comparator<ListNode> comp = new Comparator<ListNode>() {
 7             public int compare(ListNode o1, ListNode o2) {
 8                 if (o1.val < o2.val)
 9                     return -1;
10                 if (o1.val > o2.val)
11                     return 1;
12                 return 0;
13             }
14         };
15         PriorityQueue<ListNode> heap = new PriorityQueue<ListNode>(
16                 lists.size(), comp);
17         for (ListNode node : lists) {
18             if (node != null)
19                 heap.add(node);
20         }
21         ListNode head = null, cur = null;
22         while (!heap.isEmpty()) {
23             if (head == null) {
24                 head = heap.poll();
25                 cur = head;
26             } else {
27                 cur.next = heap.poll();
28                 cur = cur.next;
29             }
30             if (cur.next != null)
31                 heap.add(cur.next);
32         }
33         return head;
34     }
35 }
bubuko.com,布布扣

我的提交代码:

bubuko.com,布布扣
 1 public class Solution {
 2     public ListNode mergeKLists(List<ListNode> lists) {
 3         if (lists == null || lists.isEmpty())
 4             return null;
 5 
 6         Comparator<ListNode> comp = new Comparator<ListNode>() {
 7             public int compare(ListNode o1, ListNode o2) {
 8                 if (o1.val < o2.val)
 9                     return -1;
10                 if (o1.val > o2.val)
11                     return 1;
12                 return 0;
13             }
14         };
15         PriorityQueue<ListNode> heap = new PriorityQueue<ListNode>(
16                 lists.size(), comp);
17         for (ListNode node : lists) {
18             if (node != null)
19                 heap.add(node);
20         }
21         ListNode dummy=new ListNode(-1);
22         ListNode pointer=dummy;
23         while(!heap.isEmpty()){
24             ListNode temp=heap.peek();
25             heap.poll();
26             pointer.next=temp;
27             pointer=pointer.next;
28             if(temp.next!=null){
29                 heap.add(temp.next);
30             }
31         }
32         return dummy.next;
33     }
34 }
bubuko.com,布布扣

需要注意的是:PriorityQueue的使用方法

 PriorityQueue<ListNode> heap = new PriorityQueue<ListNode>(lists.size(), comp);
 

 

[Leetcode] Merge k Sorted Lists,布布扣,bubuko.com

[Leetcode] Merge k Sorted Lists

标签:des   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/wolohaha/p/3779615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!