-
Notifications
You must be signed in to change notification settings - Fork 0
/
23.py
27 lines (27 loc) · 1008 Bytes
/
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
"""
題目已經排序所有的ListNode, 所以依序比較第一個元素, 最小的就取出
"""
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
if all(not list_ for list_ in lists):
return None
pointer = ListNode(None)
dummy = pointer
while True:
tmp = 10 ** 4 + 1 # 題目有給限制
selected_index = 0
for index, list_ in enumerate(lists):
if list_ and list_.val < tmp:
tmp = list_.val
selected_index = index
pointer.next = lists[selected_index]
lists[selected_index] = lists[selected_index].next
pointer = pointer.next
if lists.count(None) == len(lists):
break
return dummy.next