当前位置:网站首页>Leetcode 23. 合并K个升序链表
Leetcode 23. 合并K个升序链表
2022-07-04 07:41:00 【von Libniz】
题目链接:https://leetcode.cn/problems/merge-k-sorted-lists/
题目描述
给你一个链表数组,每个链表都已经按升序排列。请你将所有链表合并到一个升序链表中,返回合并后的链表。
示例 1:
输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[
1->4->5,
1->3->4,
2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6
示例 2:
输入:lists = []
输出:[]
示例 3:
输入:lists = [[]]
输出:[]
这题显然是数组k路归并的扩展,而数组k路归并存在三种解法(假设k路数组的所有元素数为n):
(1)k路数组拷贝至同一数组,再统一排序
(2)k路数组进行k次归并
(3)使用最小堆,依次从k个数组中找到最小值,重复n次
而这题把数组换成了链表,那使用第三种方法是比较合适的。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
if not lists:
return None
k = len(lists)
dummy = ListNode(val=-1)
tail = dummy
heap = []
# put each list head to min heap
for i, node in enumerate(lists):
if node is not None:
heap.append((node.val, i, node)) # 当val相同通过link_index:i比较
heapq.heapify(heap)
# find min node n times
while len(heap) > 0:
_, idx, cur_node = heapq.heappop(heap)
tail.next = cur_node
tail = cur_node
if cur_node.next is not None:
heapq.heappush(heap, (cur_node.next.val, idx, cur_node.next))
return dummy.next
边栏推荐
- JVM -- class loading process and runtime data area
- Transition technology from IPv4 to IPv6
- L1-024 the day after tomorrow (5 points)
- SQL foundation 9 [grouping data]
- Two years ago, the United States was reluctant to sell chips, but now there are mountains of chips begging China for help
- This article is enough for learning advanced mysql
- Introduction to sap commerce cloud B2B organization function
- [Chongqing Guangdong education] National Open University spring 2019 770 real estate appraisal reference questions
- Used on windows Bat file startup project
- L1-023 output gplt (20 points)
猜你喜欢

JVM中堆概念

Go learning notes - constants

MySQL中的文本处理函数整理,收藏速查

果果带你写链表,小学生看了都说好

Linear algebra 1.1

Introduction to sap commerce cloud B2B organization function

Two years ago, the United States was reluctant to sell chips, but now there are mountains of chips begging China for help

大厂技术专家:架构设计中常用的思维模型

BUUCTF(4)

Heap concept in JVM
随机推荐
Two years ago, the United States was reluctant to sell chips, but now there are mountains of chips begging China for help
Zabbix agent主动模式的实现
Oceanbase is the leader in the magic quadrant of China's database in 2021
Introduction to neural network (Part 2)
Technical experts from large factories: common thinking models in architecture design
MySQL中的文本处理函数整理,收藏速查
[C language] open the door of C
ZABBIX monitoring system deployment
Rapidjson reading and writing JSON files
OKR vs. KPI figure out these two concepts at once!
MySQL error resolution - error 1261 (01000): row 1 doesn't contain data for all columns
促进OKR落地的工作总结该如何写?
L1-024 the day after tomorrow (5 points)
PCIe knowledge points -010: where to get PCIe hot plug data
zabbix监控系统邮件报警配置
Rhcsa the next day
Heap concept in JVM
Zephyr 学习笔记2,Scheduling
MySQL 数据库 - 函数 约束 多表查询 事务
Zephyr learning notes 1, threads