Merge K Sorted Lists
Try to solve the Merge K Sorted Lists problem.
We'll cover the following
Statement#
Given an array of sorted linked lists, your task is to merge them into a single sorted linked list and return the head of this linked list.
Constraints:
-
lists.length -
lists[i].length -
lists[i][j] - Each
lists[i]is sorted in ascending order. - The sum of all
lists[i].lengthwill not exceed .
Example#
1 of 2
2 of 2
Understand the problem#
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Merge K Sorted Lists
What is the output if the following array is given as input?
lists = [
head → 1 → 2 → 3 → NULL,
head → 12 → 56 → 200 → NULL,
head → -10 → -2 → 5 → NULL
]
head → 1 → 2 → 3 → 12 → 56 → 200 → -10 → -2 → 5 → NULL
head → 1 → 2 → 3 → -10 → -2 → 5 → 12 → 56 → 200 → NULL
head → -10 → -2 → 1 → 2 → 3 → 5 → 12 → 56 → 200 → NULL
head → -10 → -2 → 5 → 1 → 2 → 3 → 12 → 56 → 200 → NULL
Figure it out!#
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself#
Implement your solution in main.py in the following coding playground. We have provided a useful code template in the other file, that you may build on to solve this problem.
Solution: Kth Smallest Number in M Sorted Lists
Solution: Merge K Sorted Lists