当前位置:网站首页>Leetcode 23. Merge K ascending linked lists
Leetcode 23. Merge K ascending linked lists
2022-07-04 07:43:00 【von Libniz】
Topic link :https://leetcode.cn/problems/merge-k-sorted-lists/
Title Description
Here's an array of linked lists , Each list has been listed in ascending order . Please merge all the linked lists into one ascending list , Return the merged linked list .
Example 1:
Input :lists = [[1,4,5],[1,3,4],[2,6]]
Output :[1,1,2,3,4,4,5,6]
explain : The linked list array is as follows :
[
1->4->5,
1->3->4,
2->6
]
Combine them into an ordered list to get .
1->1->2->3->4->4->5->6
Example 2:
Input :lists = []
Output :[]
Example 3:
Input :lists = [[]]
Output :[]
This question is obviously an array k Expansion of road merging , And arrays k There are three solutions to path merging ( hypothesis k The number of all elements of the path array is n):
(1)k Copy the path array to the same array , And then sort them uniformly
(2)k Path array k Subordination
(3) Use the minimum heap , Successively k Minimum value found in arrays , repeat n Time
And this problem changes the array into a linked list , Then it is more appropriate to use the third method .
# 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)) # When val The same pass link_index:i Compare
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
边栏推荐
- With excellent strength, wangchain technology, together with IBM and Huawei, has entered the annual contribution list of "super ledger"!
- Relations courantes de la fiche de données d'exploitation pour les activités
- [gurobi] establishment of simple model
- 墨者学院-phpMyAdmin后台文件包含分析溯源
- 21个战略性目标实例,推动你的公司快速发展
- The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
- Leetcode (215) -- the kth largest element in the array
- ZABBIX monitoring system custom monitoring content
- 2022 - 021arts: début du deuxième semestre
- Comparison between applet framework and platform compilation
猜你喜欢

Text processing function sorting in mysql, quick search of collection

谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展

Practice (9-12 Lectures)

SQL注入测试工具之Sqli-labs下载安装重置数据库报错解决办法之一(#0{main}thrown in D:\Software\phpstudy_pro\WWW\sqli-labs-……)

弈柯莱生物冲刺科创板:年营收3.3亿 弘晖基金与淡马锡是股东

It's healthy to drink medicinal wine like this. Are you drinking it right

Amd RX 7000 Series graphics card product line exposure: two generations of core and process mix and match

Book list | as the technical support Party of the Winter Olympics, Alibaba cloud's technology is written in these books!

PCIE知识点-010:PCIE 热插拔资料从哪获取

Rhcsa the next day
随机推荐
Advanced MySQL: Basics (5-8 Lectures)
Zephyr 学习笔记2,Scheduling
人生规划(Flag)
Unity 从Inspector界面打开资源管理器选择并记录文件路径
【Go基础】2 - Go基本语句
Handwritten easy version flexible JS and source code analysis
Chrome is set to pure black
PCIe knowledge points -010: where to get PCIe hot plug data
Life planning (flag)
OKR vs. KPI 一次搞清楚这两大概念!
L1-022 odd even split (10 points)
[real case] how to deal with the failure of message consumption?
手写简易版flexible.js以及源码分析
L1-023 output gplt (20 points)
How to use MOS tube to realize the anti reverse connection circuit of power supply
How to write a summary of the work to promote the implementation of OKR?
Two years ago, the United States was reluctant to sell chips, but now there are mountains of chips begging China for help
What determines vacuum permittivity and vacuum permeability? Why do these two physical quantities exist?
Routing decorator of tornado project
Types of references in BibTex