Leetcode 23. Merge k Sorted Lists
来源:互联网 发布:淘宝买单反靠谱吗 编辑:程序博客网 时间:2024/06/05 23:48
Using PriorityQueue (Minheap).
public class Solution { // Implement comparator for ListNode private class ListNodeComparator implements Comparator<ListNode> { public int compare(ListNode l1, ListNode l2) { return l1.val - l2.val; } } public ListNode mergeKLists(ListNode[] lists) { PriorityQueue<ListNode> queue = new PriorityQueue<>(new ListNodeComparator()); ListNode dummy = new ListNode(0); ListNode tail = dummy; for (ListNode node:lists) if (node != null) queue.offer(node); while (!queue.isEmpty()){ tail.next = queue.poll(); tail = tail.next; if (tail.next != null) queue.offer(tail.next); } return dummy.next; }}
Given a list array, for example, l1 l2 l3 l4 l5 l6 l7 l8.
Intuitive solution, merge l1 and l2 into new list l1', then merge l1' and l3 into l2', similarly, merge l2' and l4 into l3'.
Time complexity O(N^2).
Divide and conquer. Divide the original lists into small pieces (i.e. two lists), merge them, and then merge the bigger one, so on and so forth. Divide and conquer will take O(logN), merge will take O(N),
total time complexity O(NlogN).
public class Solution { public ListNode mergeKLists(ListNode[] lists) { if (lists == null || lists.length == 0) { return null; } if (lists.length == 1) { return lists[0]; } // intuitive solution, takes O(n) // merge the first two lists then merge the rest lists // ListNode ret = mergeTwoLists(lists[0], lists[1]); // for (int i=2; i<lists.length; i++) { // ret = mergeTwoLists(ret, lists[i]); // } // divide and conquer, takes O(logn) return divideConquer(0, lists.length-1, lists); } private static ListNode divideConquer(int start, int end, ListNode[] lists) { if (start == end) { return lists[start]; } else if (start < end) { int mid = (end-start)/2+start; ListNode l1 = divideConquer(start, mid, lists); ListNode l2 = divideConquer(mid+1, end, lists); return mergeTwoLists(l1, l2); } return null; } // recursively merge two lists private static ListNode mergeTwoLists(ListNode list1, ListNode list2) { if (list1 == null) { return list2; } if (list2 == null) { return list1; } // head of merged lists ListNode head; if (list1.val < list2.val) { head = list1; head.next = mergeTwoLists(list1.next, list2); } else { head = list2; head.next = mergeTwoLists(list1, list2.next); } return head; }}
0 0
- leetcode 23. LeetCode Merge k Sorted Lists
- [LeetCode]23.Merge k Sorted Lists
- LeetCode --- 23. Merge k Sorted Lists
- LeetCode 23.Merge k Sorted Lists
- [Leetcode] 23. Merge k Sorted Lists
- 【LeetCode】23.Merge k Sorted Lists
- [leetcode] 23.Merge k Sorted Lists
- Leetcode-23.Merge k Sorted Lists
- [Leetcode]23. Merge k Sorted Lists @python
- leetcode 23. Merge k Sorted Lists
- leetcode 23. Merge k Sorted Lists
- leetcode 23. Merge k Sorted Lists
- LeetCode 23. Merge k Sorted Lists
- [leetcode] 23. Merge k Sorted Lists
- LeetCode 23. Merge k Sorted Lists
- LeetCode 23. Merge k Sorted Lists
- LeetCode-23.Merge k Sorted Lists
- Leetcode 23. Merge k Sorted Lists
- Leetcode 264. Ugly Number II
- gcc,g++支持c11、c++11
- C语言实现修改文本文件中特定行的实现代码
- web.xml部署描述符
- Unity 3D 实现拾取物品功能(一)
- Leetcode 23. Merge k Sorted Lists
- 欢迎使用CSDN-markdown编辑器
- 【Leetcode】282. Expression Add Operators
- Leetcode-401. Binary Watch
- java网络编程三要素及socket编程
- LeetCode: Single Number
- VS2010+SQlite+EF 三、再深入一点
- Jquery zTree实例
- 简单设计类