Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Input:
[
1->4->5,
1->3->4,
2->6
]
Output: 1->1->2->3->4->4->5->6
-
Time complexity :
O(n * log(k))
. We divide each problem by half. Making itlog(k)
steps. In each step we doO(n)
work. Wheren
is the number of nodes in a linked-list. Same asmerge sort
complexity analysis. -
Space complexity :
O(log(k))
. Stack space to keep on dividing in half. Same asmerge sort
complexity analysis.