-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23.MergeKSortedLists.py
More file actions
46 lines (43 loc) · 1.29 KB
/
23.MergeKSortedLists.py
File metadata and controls
46 lines (43 loc) · 1.29 KB
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
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
:note:
跟合并两个有序链表没啥区别,为了节省时间,需要动态删除lists节点,因此需要改用while循环
"""
#lists[0] = ListNode(1)
#lists[1] = ListNode(0)
#lists.pop()
l = len(lists)
if l == 0:
return None
if l == 1:
return lists[0]
head = pre = None
while(l > 0):
minI, minV = -1, float('inf')
i = 0
while i < l:
if lists[i] == None:
l -= 1
del lists[i]
continue
if lists[i].val < minV:
minV = lists[i].val
minI = i
i += 1
if minI == -1:
return head
if head == None:
head = pre = lists[minI]
else:
pre.next = lists[minI]
pre = lists[minI]
lists[minI] = lists[minI].next
return head