Merge K Sorted Linked Lists

Naveen Jetty
Naveen Jetty
Published in
1 min readJan 20, 2017

--

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Sample Input:
1 -> 2
3 -> 4
Sample Output:
1 -> 2 -> 3 -> 4

Solution:

Explanation:

I am using the priority queues which uses a binary heap to sort its elements based on the comparator. So the time complexity of this logic is O(n log k) where k is the number of input linked lists and n is the total number of elements in the final linked list.

--

--

If it's not solving all your problems, you simply aren't using enough of it..!!