Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example
Input:
[
1->4->5,
1->3->4,
2->6
]
Output: 1->1->2->3->4->4->5->6
O(nklogk) runtime, O(k) space – Heap
1 | public class ListNode { |
1 | public ListNode mergeKLists(ListNode[] lists) { |
O(nklogk) runtime, O(1) space – Divide and conquer using two way merge
1 | public class ListNode { |
1 | public ListNode mergeKLists(ListNode[] lists) { |