-
Notifications
You must be signed in to change notification settings - Fork 0
/
h23_merge_k_sorted_lists.py
38 lines (30 loc) · 1.03 KB
/
h23_merge_k_sorted_lists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/python
# PROGRAMMER: Luke Wilson
# DATE CREATED: 2022-04-17
# Question: https://leetcode.com/problems/merge-k-sorted-lists/
##
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
sorted_list = sorter = ListNode()
nodes = dict()
for index in range(len(lists)):
if lists[index]:
nodes[index] = lists[index]
while nodes:
min_value = float("inf")
for index, node in nodes.items():
if node.val < min_value:
min_value = node.val
min_index = index
sorter.next = nodes[min_index]
sorter = sorter.next
if nodes[min_index].next:
nodes[min_index] = nodes[min_index].next
else:
nodes.pop(min_index)
return sorted_list.next