Leetcode: Merge k Sorted Lists
2014-11-17 16:24
162 查看
Merge k sorted
linked lists and return it as one sorted list. Analyze and describe its complexity.
Divide and conquer: Divide the list into two parts, and use recursion to divide the two parts and merge. Time complexity
O(nklogk), n is the length of the longest linked list. Space complexity is the heap size O(logk)
/**
* 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;
}
if (lists.size() == 1) {
return lists.get(0);
}
ListNode p1 = mergeKLists(lists.subList(0, lists.size() / 2));
ListNode p2 = mergeKLists(lists.subList(lists.size() / 2, lists.size()));
return merge2Lists(p1, p2);
}
private ListNode merge2Lists(ListNode p1, ListNode p2) {
if (p1 == null) return p2;
ListNode dummy = new ListNode(0);
dummy.next = p1;
ListNode h1 = dummy;
ListNode h2 = h1.next;
while (p2 != null) {
if (h2 == null) {
h1.next = p2;
break;
}
if (p2.val < h2.val) {
ListNode temp = p2.next;
h1.next = p2;
p2.next = h2;
p2 = temp;
}
h1 = h1.next;
h2 = h1.next;
}
return dummy.next;
}
}
linked lists and return it as one sorted list. Analyze and describe its complexity.
Divide and conquer: Divide the list into two parts, and use recursion to divide the two parts and merge. Time complexity
O(nklogk), n is the length of the longest linked list. Space complexity is the heap size O(logk)
/**
* 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;
}
if (lists.size() == 1) {
return lists.get(0);
}
ListNode p1 = mergeKLists(lists.subList(0, lists.size() / 2));
ListNode p2 = mergeKLists(lists.subList(lists.size() / 2, lists.size()));
return merge2Lists(p1, p2);
}
private ListNode merge2Lists(ListNode p1, ListNode p2) {
if (p1 == null) return p2;
ListNode dummy = new ListNode(0);
dummy.next = p1;
ListNode h1 = dummy;
ListNode h2 = h1.next;
while (p2 != null) {
if (h2 == null) {
h1.next = p2;
break;
}
if (p2.val < h2.val) {
ListNode temp = p2.next;
h1.next = p2;
p2.next = h2;
p2 = temp;
}
h1 = h1.next;
h2 = h1.next;
}
return dummy.next;
}
}
相关文章推荐
- Leetcode-21 Merge Two Sorted Lists
- leetcode-21. Merge Two Sorted Lists
- leetcode 21 Merge Two Sorted Lists
- LeetCode 23. Merge k Sorted Lists [hard]
- leetcode 23. Merge k Sorted Lists
- Leetcode 21. Merge Two Sorted Lists
- LeetCode: Merge two sorted linked lists
- leetcode -- Merge k Sorted Lists -- 重点有trick
- leetcode--Merge Two Sorted Lists
- LeetCode|Merge Two Sorted Lists
- [LeetCode]Merge Two Sorted Lists
- LeetCode: Merge Two Sorted Lists, Remove Duplicates/Element, strStr()
- leetcode挨个儿刷150105(1):Merge Two Sorted Lists
- leetcode_021 Merge Two Sorted Lists
- LeetCode - Merge k Sorted Lists
- Leetcode Merge Two Sorted Lists 解题报告
- leetcode Merge k Sorted Lists
- (leetcode)Merge Two Sorted Lists
- LeetCode----Merge Two Sorted Lists
- leetcode-23-Merge k Sorted Lists