Heap

class Solution(object):
def mergeKLists(self, lists):
curr = dummy = ListNode(None)
heap = [(node.val, i, node) for i,node in enumerate(lists) if node is not None]
i = len(lists)
heapq.heapify(heap)
while heap:
_, _,node = heapq.heappop(heap)
curr.next = node
curr = node
if node.next is not None:
heapq.heappush(heap, (node.next.val, i, node.next))
i += 1
return dummy.next

--

--

--

Data Scientist/MLE/SWE @takemobi

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The checklist for your daily standups, part I.

Build a Bot: Data-powered conversations

Redis as a Distributed Cache on .NET 6.0

Azure — Why Annoying?

Installing Docker Engine 19.03 on Raspberry Pi 3 in 2 Minutes

The How and Why of Reversible Tokenization

It’s Okay If You’ve Never Been To An Anime Convention or Played DnD, You Can Still Be A Coder

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jimmy Shen

Jimmy Shen

Data Scientist/MLE/SWE @takemobi

More from Medium

Designing an Engineering Take Home Assignment — Part A

A pen lying on a white sheet of paper with some equations on it

Leetcode 706. Design HashMap

Leetcode Q492. Construct the Rectangle (Q416)

5/4 Binary Search Tree