LeetCode Merge k Sorted Lists
2014-12-26 23:16
253 查看
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode mergeKLists(List<ListNode> lists) { if (lists == null || lists.size() == 0) { return null; } Comparator<ListNode> compare=new Comparator<ListNode>() { @Override public int compare(ListNode o1, ListNode o2) { return o1.val - o2.val; } }; PriorityQueue<ListNode> queue = new PriorityQueue<ListNode>(lists.size(), compare); for (ListNode node : lists) { if (node != null) { queue.add(node); } } ListNode ret = new ListNode(0); ListNode temp = ret; while (!queue.isEmpty()) { temp.next=queue.poll(); temp=temp.next; if (temp.next != null) { queue.add(temp.next); } } return ret.next; } }
相关文章推荐
- leetcode_23_Merge k Sorted Lists
- [leetcode]23 Merge k Sorted Lists
- leetcode Merge k Sorted Lists
- LeetCode之“链表”:Merge Two Sorted Lists && Merge k Sorted Lists
- LeetCode之Merge Two Sorted Lists
- Leetcode|Merge k Sorted Lists
- Leetcode Merge Two Sorted Lists
- leetcode:Merge Two Sorted Lists 【Java】
- [leetcode] Merge Two Sorted Lists
- LeetCode-23. Merge k Sorted Lists
- leetcode -- Merge Two Sorted Lists
- 【Leetcode】Merge Two Sorted Lists in JAVA
- LeetCode 23. Merge k Sorted Lists
- [leetcode]#21. Merge Two Sorted Lists
- leetcode Merge Two Sorted Lists
- [leetcode:python]21.Merge Two Sorted Lists
- Merge Two Sorted Lists leetcode java
- leetcode 23. Merge k Sorted Lists
- Leetcode 21 Merge Two Sorted Lists
- Leetcode:Merge Two Sorted Lists(Importance!)