-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.py
86 lines (73 loc) · 2.3 KB
/
23.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def create_linked_list(arr):
head = ListNode(arr[0])
cur = head
for i in range(1, len(arr)):
cur.next = ListNode(arr[i])
cur = cur.next
return head
def print_linked_list(head):
cur = head
res = []
while cur:
res.append(cur.val)
cur = cur.next
return res
class Solution():
def mergeKLists(self, lists):
K = len(lists)
head = ListNode(0)
p = head
k_heads = lists[:]
while any([k_head is not None for k_head in k_heads]):
min = float('inf')
temp = None
for k_head in k_heads:
# k_head = k_heads[i]
if k_head is None:
continue
if k_head.val <= min:
temp = k_head
min = k_head.val
# k_head = k_head.next
for i in range(len(k_heads)):
k_head = k_heads[i]
if k_head is temp:
k_heads[i] = k_head.next
p.next = temp
p = p.next
return head.next
class Solution2():
def mergeKLists(self, heads):
head = ListNode(-1)
heads_vaild = [x for x in heads if x is not None]
q = head
while any(x for x in heads_vaild if x is not None):
temp = None
temp_id = -1
for i, head_inner in enumerate(heads_vaild):
if head_inner is None:
continue
if temp is None:
temp = head_inner
temp_id = i
continue
if head_inner.val < temp.val:
temp = head_inner
temp_id = i
q.next = temp
q = q.next
if heads_vaild[temp_id] is None:
continue
heads_vaild[temp_id] = heads_vaild[temp_id].next
return head.next
if __name__ == "__main__":
solu = Solution2()
lists = [[1, 4, 5], [1, 3, 4], [2, 6]]
heads = [create_linked_list(item) for item in lists]
# print(any([k_head is not None for k_head in heads]))
new_head = solu.mergeKLists(heads)
print(print_linked_list(new_head))