 # Hard Interview Question (Suboptimal Solution)

In this lesson, you’ll see an attempt at improving the brute force solution. You’ll see how this solution is still suboptimal, but it’s important to understand before moving on to the optimal solution. The suboptimal solution will be available at the end of the course. ambreen2006

Thank you for this wonderful tutorial. I’m finding it very useful.

I think in the sub-optimal solution provided in the video + code, there is no need to separately iterate for the min value and the index to which the min val belongs to. So I can write something like this:

``````    head_ptr_list = linked_lists[:]
ptr = result

if node]
min_val, min_index = min(heads, key = lambda x: x)
ptr = ptr.next

return result.next
``````

But please do correct me if I overlook something. mdwikira

My solution for the suboptimal version.

``````class Link:
def __init__(self, val, next=None):
self.val = val
self.next = next

def __str__(self):
if not self.next:

'''
Merge k sorted linked lists into one
... ]))
... ]))
'''
result = None
last = None
while ll[0:]:
smallest = min(ll, key=lambda x: x.val)
ll.remove(smallest)

if smallest.next:
ll.append(smallest.next)
smallest.next = None

if result == None:
last = smallest
result = last
continue

last.next = smallest
last = last.next

return result
``````

to join the conversation.

Lesson Completed!
Lesson Bookmarked
Request Failed :(