Prepin
Log in

G

Combine K sorted linked lists.

Approach / Explanation

This inquiry pertains to data structures, specifically linked lists and mergesort. A linked list is a type of data structure made up of a series of elements linked together. It is the second most commonly utilized data structure after arrays. Merge sort is an algorithm based on the divide-and-conquer principle. Both of these concepts are essential in the field of computer science. In this scenario, you are provided with K sorted linked lists, each organized in ascending order. Your objective is to combine all of these linked lists into a single linked list that maintains the ascending order. You can proceed with the assumption that all elements are unique. Your explanation should outline a step-by-step method to tackle this problem, presuming that the interviewer is familiar with linked lists and merge sort.

Suggested Answer

A straightforward approach would involve adding all nodes to a list in Python, sorting that list, and then converting it back into a linked list. This method would lead to a time complexity of O(n log n), where n represents the total number of nodes, due to Python's sorting algorithm, TimSort, which combines Insertion Sort and Merge Sort. Here’s an illustration of the naive solution in Python: class Solution ( object ) : def mergeKLists ( self , lists ) : self . nodes = [ ] head = point = ListNode ( 0 ) for l in lists : while l : self . nodes . append ( l . val ) l = l . next for x in sorted ( self . nodes ) : point . next = ListNode ( x ) point = point . next return head . next

Alternative Answer

Rather than creating a list and sorting it, a more efficient approach would be to utilize a priority queue to pick the node with the smallest value at each iteration. This leads to a time complexity of O(n log k), which is advantageous since k is typically much smaller than n. Below is the Python code for this method: from Queue import PriorityQueue class Solution ( object ) : def mergeKLists ( self , lists ) : head = point = ListNode ( 0 ) q = PriorityQueue ( ) for l in lists : if l : q . put ( ( l . val , l ) ) while not q . empty ( ) : val , node = q . get ( ) point . next = ListNode ( val ) point = point . next node = node . next if node : q . put ( ( node . val , node ) ) return head . next This code initializes a PriorityQueue, and for each list, it inserts the first node into the queue. A while loop then runs until the queue is empty. In each loop iteration, the node with the smallest value is removed from the queue and added to the resulting linked list. If the removed node has a subsequent node, it is also added to the queue. The priority queue organizes the nodes based on their values, ensuring that nodes are extracted in ascending order.

Question Details

Difficulty & Category

Medium

Companies

Meta (Facebook)
Google

Product

AI Candidate AgentCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin | All rights reserved.