当前位置:网站首页>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
边栏推荐
- 真空介电常数和真空磁导率究竟是由什么决定的?为何会存在这两个物理量?
- 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)
- 猜数字游戏
- One of the general document service practice series
- The text box displays the word (prompt text) by default, and the text disappears after clicking.
- Zephyr Learning note 2, Scheduling
- 2022-021ARTS:下半年開始
- MySQL error resolution - error 1261 (01000): row 1 doesn't contain data for all columns
- Activiti common operation data table relationship
- Jianmu continuous integration platform v2.2.2 release
猜你喜欢
Used on windows Bat file startup project
MySQL中的文本處理函數整理,收藏速查
PCIE知识点-010:PCIE 热插拔资料从哪获取
Oracle-存储过程与函数
[real case] how to deal with the failure of message consumption?
Handwritten easy version flexible JS and source code analysis
Email alarm configuration of ZABBIX monitoring system
ZABBIX monitoring system custom monitoring content
神经网络入门(下)
[network security] what is emergency response? What indicators should you pay attention to in emergency response?
随机推荐
Div hidden in IE 67 shows blank problem IE 8 is normal
How to use MOS tube to realize the anti reverse connection circuit of power supply
Unity opens the explorer from the inspector interface, selects and records the file path
墨者学院-Webmin未经身份验证的远程代码执行
Comparison between applet framework and platform compilation
Advanced MySQL: Basics (5-8 Lectures)
Node foundation ~ node operation
[network security] what is emergency response? What indicators should you pay attention to in emergency response?
The IP bound to the socket is inaddr_ The meaning of any htonl (inaddr_any) (0.0.0.0 all addresses, uncertain addresses, arbitrary addresses)
Tri des fonctions de traitement de texte dans MySQL, recherche rapide préférée
Zabbix agent主动模式的实现
Zephyr 学习笔记2,Scheduling
Introduction to rce in attack and defense world
Would you like to go? Go! Don't hesitate if you like it
21 examples of strategic goals to promote the rapid development of your company
SQL注入测试工具之Sqli-labs下载安装重置数据库报错解决办法之一(#0{main}thrown in D:\Software\phpstudy_pro\WWW\sqli-labs-……)
Rapidjson reading and writing JSON files
How to write a summary of the work to promote the implementation of OKR?
Oceanbase is the leader in the magic quadrant of China's database in 2021
It's healthy to drink medicinal wine like this. Are you drinking it right