-
Notifications
You must be signed in to change notification settings - Fork 0
/
023_merge-k-sorted-lists.py
97 lines (87 loc) · 2.67 KB
/
023_merge-k-sorted-lists.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
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#@author: rye
#@time: 2019/4/28
# 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
"""
if lists == []:
return []
L = len(lists)
res = lists[0]
for i in range(1, L):
res = self.mergeTwoLists(res, lists[i])
return res
def mergeTwoLists(self, l1, l2):
ans = ListNode(0)
res = ans
while l1 and l2:
if l1.val < l2.val:
res.next = l1
res = res.next
l1 = l1.next
else:
res.next = l2
res = res.next
l2 = l2.next
if l1:
res.next = l1
if l2:
res.next = l2
return ans.next
class Solution1(object):
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
heap = []
import heapq
for node in lists:
if node:
heap.append((node.val, node)) # 堆中放入tuple:值,地址
heapq.heapify(heap) # 做成堆
dummy = ListNode(0)
curr = dummy
while heap:
pop = heapq.heappop(heap) # 从堆中取最小的值
curr.next = ListNode(pop[0]) # 将值放入
curr = curr.next
if pop[1].next: # 如果该链表没结束
heapq.heappush(heap, (pop[1].next.val, pop[1].next)) # 将该链表下个节点压入栈中
return dummy.next
# ---------------------
# 作者:Rude3knife
# 来源:CSDN
# 原文:https://blog.csdn.net/qqxx6661/article/details/77814794
# 版权声明:本文为博主原创文章,转载请附上博文链接!
# 法3 用这个最取巧了!
class Solution3(object):
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
nlist=[]
for node in lists:
while(node):
nlist.append(node.val)
node=node.next
nlist.sort()
head=ListNode(None)
temp=head
for i in nlist:
temp.next=ListNode(i)
temp=temp.next
return head.next
if __name__ == '__main__':
pass