-
Notifications
You must be signed in to change notification settings - Fork 0
/
23_合并K个排序链表.py
54 lines (46 loc) · 1.22 KB
/
23_合并K个排序链表.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
"""
合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
示例:
输入:
[
1->4->5,
1->3->4,
2->6
]
输出: 1->1->2->3->4->4->5->6
"""
"""
解题思路:归并排序的思想
"""
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
res=ListNode(None)
Node=res
while l1 and l2:
if l1.val<l2.val:
Node.next,l1=l1,l1.next
else:
Node.next,l2=l2,l2.next
Node=Node.next
if l1:
Node.next=l1
if l2:
Node.next=l2
return res.next
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
length=len(lists)
if length==0:
return None
if length==1:
return lists[0]
if length==2:
return self.mergeTwoLists(lists[0],lists[1])
mid=length//2
leftLists=self.mergeKLists(lists[:mid])
rightLists=self.mergeKLists(lists[mid:])
return self.mergeTwoLists(leftLists,rightLists)