当前位置:网站首页>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
边栏推荐
- Practice (9-12 Lectures)
- Project 1 household accounting software (goal + demand description + code explanation + basic fund and revenue and expenditure details record + realization of keyboard access)
- Activiti common operation data table relationship
- A real penetration test
- Zephyr learning notes 1, threads
- 线性代数1.1
- Blue Bridge Cup Quick sort (code completion)
- Basic DOS commands
- L1-026 I love gplt (5 points)
- Two years ago, the United States was reluctant to sell chips, but now there are mountains of chips begging China for help
猜你喜欢

Sqli labs download, installation and reset of SQL injection test tool one of the solutions to the database error (# 0{main}throw in d:\software\phpstudy_pro\www\sqli labs-...)

zabbix 5.0监控客户端

Devops Practice Guide - reading notes (long text alarm)

Unity 从Inspector界面打开资源管理器选择并记录文件路径

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

How to improve your system architecture?

Zephyr learning notes 1, threads

Common components of flask

Implementation of ZABBIX agent active mode

In the era of low code development, is it still needed?
随机推荐
2022 - 021arts: début du deuxième semestre
Do you know about autorl in intensive learning? A summary of articles written by more than ten scholars including Oxford University and Google
Tri des fonctions de traitement de texte dans MySQL, recherche rapide préférée
Detailed introduction to the big changes of Xcode 14
Experience installing VMware esxi 6.7 under VMware Workstation 16
JVM中堆概念
Activiti common operation data table relationship
Rhcsa day 3
University stage summary
Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
2022-021rts: from the second half of the year
zabbix监控系统部署
Rhcsa the next day
MYCAT middleware installation and use
Go learning notes - constants
Life planning (flag)
谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展
What determines vacuum permittivity and vacuum permeability? Why do these two physical quantities exist?
时序数据库 InfluxDB 2.2 初探
SQL注入测试工具之Sqli-labs下载安装重置数据库报错解决办法之一(#0{main}thrown in D:\Software\phpstudy_pro\WWW\sqli-labs-……)