Home » Java » Leetcode ***Merge K Sorted Lists twenty-third
subject
Mergeksorted
Linked, lists, and, return, it, as, one, sorted, list., Analyze,, and, describe, its, complexity.

ideas
the K ordered list in the array is merged into an ordered list, and the time complexity is analyzed. This problem can be designed on the basis of twenty-second questions - "merge two sorted lists",.

began to think of an array of traversal k-1 a list of the first list and second list merged into a new list, and then the third list with this new list merge into a new list, and so on... That... The list length up to N, this design of the time complexity of the algorithm is O (n*k), submit a timeout error.

change ideas, do not use a single traversal. Can recursive merge in an array of K a list will be divided into k/2 size array merge, and then divided into k/2^2 size array merge... This recursive merge... The time complexity is O (logK), the total time complexity is O (n*logk)

code
Python
#, Definition, for, singly-linked, list.
# class ListNode (object):
# def __init__ (self, X):
# self.val = x
# self.next = None
Class Solution (object):
Def mergeKLists (self, lists):
"""
: type, lists:, List[ListNode]
: rtype: ListNode
"""
If (len (lists) = = 0):
Return
Return, self.merge (lists, 0, len (lists) - 1)
Def, merge (self, lists, start, end):
If (start = end):
Return lists[start]
If (end - start = = 1):
Return self.mergeTwoLists (lists[start], lists[end])
Mid = start + (end - start) /2
Return, self.mergeTwoLists (self.merge (lists, start, mid), self.merge (lists, mid+1, end))
Def, mergeTwoLists (self, L1, L2):
"""
: type, l1:, ListNode
: type, l2:, ListNode
: rtype: ListNode
"""
Head = ListNode (0)
While (L1 = = None, and, L2 = None):
Node # traversal L1 and L2 and compare the size, will be small in the target list, cur cursor
If (l1.val < l2.val):
Cur.next = L1
L1 = l1.next
Else:
Cur.next = L2
L2 = l2.next
Cur.next.next = None
Cur = cur.next
After comparing L1 and L2 # left node
If (L1 = = None):
Cur.next = L1
Else:
Cur.next = L2

Java
/**
* Definition, for, singly-linked, list.
* public, class, ListNode {
* int val;
* ListNode next;
* ListNode (int, x) {Val = x}
*}
* /
Public, class, Solution {
Public, ListNode, mergeKLists (ListNode[], lists) {
If (lists.length = 0)
Return null;
Return merge (lists, 0, lists.length - 1); / / also defines a function according to the subscript merge
}
Public, ListNode, merge (ListNode[], lists, int, start, int, end) {
If (start = end)
Return lists[start];
If (end - start = = 1)
Return mergeTwoLists (lists[start], lists[end]);
Int mid = start + (end - start) / 2;
Merge / / recursive merge, k/2, k/2^2, k/2^3... Until merge two or a
Return, mergeTwoLists (merge (lists, start, mid), merge (lists, mid + 1, end));
}
Public, ListNode, mergeTwoLists (ListNode, L1, ListNode, L2) {
ListNode head = new ListNode (0);
ListNode cur = head;
L1 and L2 / node traversal and compare the size of cur, on behalf of the current node
While (L1! = null & & L2! = null) {
If (l1.val < l2.val) {
Cur.next = l1;
L1 = l1.next;
} else {
Cur.next = l2;
L2 = l2.next;
}
Cur.next.next = null; / / can reduce the list size, improve the efficiency of
Cur = cur.next;
}
Comparison of L1 and L2 / / end node of the left node
If (L1 = = null)
Cur.next = l1;
Else
Cur.next = l2;