leetcode--Merge k Sorted Lists
2017-08-08 09:00
106 查看
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
[java] view
plain copy
/**
* Definition for singly-linked list.
* public class ListNode {
* i
4000
nt val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeKLists(ListNode[] lists) {
Queue<ListNode> heap = new PriorityQueue<ListNode>(new Comparator<ListNode>(){
@Override public int compare(ListNode l1, ListNode l2) {
return l1.val - l2.val;
}
});
ListNode head = new ListNode(0), tail = head;
for (ListNode node : lists) if (node != null) heap.offer(node);
while (!heap.isEmpty()) {
tail.next = heap.poll();//获取最大值
tail = tail.next;
if (tail.next != null) heap.offer(tail.next);
}
return head.next;
}
}
原文链接http://blog.csdn.net/crazy__chen/article/details/45581225
[java] view
plain copy
/**
* Definition for singly-linked list.
* public class ListNode {
* i
4000
nt val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeKLists(ListNode[] lists) {
Queue<ListNode> heap = new PriorityQueue<ListNode>(new Comparator<ListNode>(){
@Override public int compare(ListNode l1, ListNode l2) {
return l1.val - l2.val;
}
});
ListNode head = new ListNode(0), tail = head;
for (ListNode node : lists) if (node != null) heap.offer(node);
while (!heap.isEmpty()) {
tail.next = heap.poll();//获取最大值
tail = tail.next;
if (tail.next != null) heap.offer(tail.next);
}
return head.next;
}
}
原文链接http://blog.csdn.net/crazy__chen/article/details/45581225
相关文章推荐
- LeetCode "Merge Two Sorted Lists"
- leetcode[23]Merge k Sorted Lists
- LeetCode | 23. Merge k Sorted Lists
- [LeetCode]Merge Two Sorted Lists
- LeetCode: Merge Two Sorted Lists
- LeetCode题解:Merge Two Sorted Lists
- LeetCode: MergekSortedLists
- Leetcode--Merge k Sorted Lists
- LeetCode 23. Merge k Sorted Lists
- leetcode Merge k Sorted Lists
- LeetCode-21-Merge-Two-Sorted-Lists 递归水题
- LeetCode_Merge Two Sorted Lists
- LeetCode 23. Merge k Sorted Lists
- [LeetCode] Merge k Sorted Lists 解题报告
- [LeetCode]Merge k Sorted Lists
- leetcode 27: Merge k Sorted Lists
- leetcode刷题日记——Merge Two Sorted Lists
- Leetcode: Merge Two Sorted Lists
- LeetCode Merge Two Sorted Lists
- LeetCode 88 Merge Two Sorted Lists