-
-
Save paramsingh/668612540dca1d14d35ea5414fc68795 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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