-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeKSortedLists.py
43 lines (41 loc) · 1.24 KB
/
MergeKSortedLists.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
39
40
41
42
43
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def mergeKLists(lists) -> ListNode:
if not len(lists): return None
elif len(lists) == 1: return lists[0]
d = {}
front = None
while len(lists):
i = 0
while i < len(lists):
if lists[i]:
n = ListNode(lists[i].val)
if lists[i].val not in d:
d[lists[i].val] = [n, n]
else:
d[lists[i].val][1].next = n
d[lists[i].val][1] = n
lists[i] = lists[i].next
if lists[i] is None:
del lists[i]
i -= 1
i += 1
for j in sorted(d.keys(),reverse=True):
if front is None:
front = d[j][0]
else:
d[j][1].next = front
front = d[j][0]
return front
if __name__ == "__main__":
#mergeKLists([None, None, None])
l1 = ListNode(2, ListNode(2, ListNode(4)))
l2 = ListNode(1, ListNode(3, ListNode(4)))
l3 = ListNode(7, ListNode(8, ListNode(9)))
out = mergeKLists([l1, l2, l3])
while out:
print(out.val)
out = out.next