-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23. Merge k Sorted Lists.py
More file actions
37 lines (33 loc) · 1.06 KB
/
23. Merge k Sorted Lists.py
File metadata and controls
37 lines (33 loc) · 1.06 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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
if len(lists)<2:
if len(lists)<1: return None
return lists[0]
result=ListNode(0)
head=result
while True:
sign=False
for i in range(len(lists)):
if lists[i]!=None: break
minplace=i
for i in range(len(lists)):
if lists[i]!=None:
sign=True
if lists[i].val<lists[minplace].val:
minplace=i
if not sign: break
head.next=ListNode(lists[minplace].val)
lists[minplace]=lists[minplace].next
head=head.next
return result.next
# sign=False
# for i in range(len(lists)):
# if lists[i]!=None:
# sign=True
# break
#