This repository for my personal design solution for solve the leetcode Merge K Sorted List in golang
You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
Merge all the linked-lists into one sorted linked-list and return it.
Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: The linked-lists are:
[
1->4->5,
1->3->4,
2->6
]
merging them into one sorted list:
1->1->2->3->4->4->5->6
Input: lists = []
Output: []
Input: lists = [[]]
Output: []
Because Each list is sorted
Each time take each non-empty list first element to find the smallest
move this smallest from the list to the merged list
until all the list element has been move the the new merge list
because each time has k element to find smallest -> Compare k times
and total need to find N elements
consume : k * N
We could refine the My First Solution with Priority Queue(Min-Heap)
This will reduce the each compare time to log(k)
and total need to find N elements
consume : log(k) * N