Today's challenge was not simple, but I solved it with a simple method that might not be the best solution.
The challenge was to merge K sorted list into one sorted list. I listed some of the examples from the challenge. If you need more description about the challenge, you can find more detail here.
Example 1:
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
Example 2:
Input: lists = []
Output: []
Example 3:
Input: lists = [[]]
Output: []
The solution
I first merged all the linked list elements into one list and sorted them. Then I formed a new linked list out of the sorted list.
The result
Runtime: 133 ms, faster than 80.74% of Python3 online submissions for Merge k Sorted Lists.
Memory Usage: 18.3 MB, less than 28.93% of Python3 online submissions for Merge k Sorted Lists.