-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergekSortedLists.py
71 lines (70 loc) · 2.01 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from typing import Optional
from typing import 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]:
lists.sort(key = lambda x: x.val if x else 10001)
print(lists)
if not lists:
return None
if not lists[0]:
return None
while lists[-1] == None:
lists = lists[:-1]
head = tail = ListNode()
while lists:
if (len(lists) == 1):
tail.next = lists[0]
return head.next
while lists[0].val <= lists[1].val:
tail.next = lists[0]
tail = tail.next
lists[0] = lists[0].next
if not lists[0]:
break
if not lists[0]:
lists = lists[1:]
continue
else:
if lists[0].val >= lists[-1].val:
lists = lists[1:] + lists[:1]
else:
l, r = 1, len(lists) - 1
while l < r:
if lists[0].val < lists[(l + r) // 2].val:
r = (l + r) // 2
else:
l = (l + r) // 2 + 1
lists.insert(l, lists[0])
lists = lists[1:]
return head.next
fin = open("oo.xx", "r")
fout = open("xx.oo", "w")
k = int(fin.readline())
a = []
for _ in range(k):
a.append([int(x) for x in fin.readline().split(',')])
a.append([])
a.append(None)
def convert(x):
if not x:
return None
else:
tempH = tempT = ListNode()
for num in x:
tempT.next = ListNode(num)
tempT = tempT.next
return tempH.next
lists = []
for x in a:
lists.append(convert(x))
out = Solution().mergeKLists(lists)
while out:
print(out.val)
fout.write(str(out.val))
out = out.next