DEV Community

Abubakar Sadiq Ismail
Abubakar Sadiq Ismail

Posted on

Day 9 I4G 10daysofcodechallenge

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.

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

Problem Category Hard!!!, Omo

The first idea I got was to merge the lists to a single list and the sort the whole merged lists, using a sorting algorithm, like merge sort.
Think I think of complexity and how to simplify it.
Which is why should I merge the list all.
I then get the first list,
combine the first with second link sorted,
continuously until I reach the end of the list.

Complexity is O(n+k)
Space Complexity O(k)

Problem inn leetcode
Solution Implementation in js

Top comments (0)