- given k linked lists
- each list is sorted
- merge all linked lists into one sorted linked list and return it
number of lists can be between 0 and 10^4
- naive inefficient approach:
- for each node in every k list we could insert the values into a priority queue
- once the priority queue is fully populated we could construct a new list and construct a new node for each element in the queue
- optimization:
- maybe instead of constructing new nodes we can create a custom compare funciton for the priority queue that orders the nodes themselves rather than the values and we just assign the pointers in the reconstruction phase
π‘ Explanation of Solution
same as optimized approach
β Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)