Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:
最优解法,Merge with Divide And Conquer,
这种解法通过把list里的链表两两合并,把时间复杂度降到了O(nLogk),k是一共有几个pair两两合并
Time complexity : O(N\log k)O(Nlogk) where \text{k}k is the number of linked lists.
We can merge two sorted linked list in O(n)O(n) time where nn is the total number of nodes in two lists.
Sum up the merge process and we can get: O\big(\sum_{i=1}^{log_{2}{k}}N \big)= O(N\log k)O(∑i=1log2kN)=O(Nlogk)
Space complexity : O(1)O(1)
We can merge two sorted linked lists in O(1)O(1) space.
Last updated