Skip to content

Instantly share code, notes, and snippets.

@paramsingh
Created January 6, 2022 23:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
"""
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"""
def merge_k_lists(lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
if not lists:
return None
dummy = ListNode(0)
curr = dummy
while lists:
min_node = min(lists, key=lambda x: x.val)
curr.next = min_node
curr = curr.next
lists.remove(min_node)
if min_node.next:
min_node = min_node.next
else:
break
return dummy.next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment