23. Merge k Sorted Lists

solution1 - use divide and conquer

divide and conquer => logk, k is lists[] length, split to 2 part

merge O(N), N is all nodes in two lists

, so O(Nlogk)

notice, in this case, should return null => . output: []

假設只有兩個數, index 0, 1

所以 mid = 0+1)/2 = 0

l1 = partition(lists, 0,0) = ( s == e return lists[s] ) = list[0]

l2 = partition(lists, 0+1,1) = ( s == e return lists[s] ) = list[1]

return merge(list[0], list[1])

solution2 - use priorityQueue

easier solution!

Last updated

Was this helpful?