Skip to content

Commit 3e75bc8

Browse files
committed
add 23.合并k个排序链表.py
1 parent 9e7cf57 commit 3e75bc8

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

hard/.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# @lc app=leetcode.cn id=23 lang=python3
3+
#
4+
# [23] 合并K个排序链表
5+
#
6+
7+
# @lc code=start
8+
# Definition for singly-linked list.
9+
# class ListNode(object):
10+
# def __init__(self, x):
11+
# self.val = x
12+
# self.next = None
13+
14+
class Solution(object):
15+
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
16+
if not lists or len(lists) == 0:
17+
return None
18+
import heapq
19+
heap = []
20+
for node in lists:
21+
while node:
22+
heapq.heappush(heap, node.val)
23+
node = node.next
24+
dummy = ListNode(None)
25+
cur = dummy
26+
while heap:
27+
temp_node = ListNode(heappop(heap))
28+
cur.next = temp_node
29+
cur = temp_node
30+
return dummy.next
31+
32+
# @lc code=end
33+

0 commit comments

Comments
 (0)