Skip to content

Instantly share code, notes, and snippets.

@paramsingh
Created January 6, 2022 23:20
Show Gist options
  • Save paramsingh/668612540dca1d14d35ea5414fc68795 to your computer and use it in GitHub Desktop.
Save paramsingh/668612540dca1d14d35ea5414fc68795 to your computer and use it in GitHub Desktop.
"""
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