当前位置:网站首页>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
边栏推荐
- Linear algebra 1.1
- socket inet_ pton() inet_ Ntop() function (a new network address translation function, which converts the expression format and numerical format to each other. The old ones are inet_aton(), INET_ ntoa
- [real case] how to deal with the failure of message consumption?
- MySQL error resolution - error 1261 (01000): row 1 doesn't contain data for all columns
- SQL注入测试工具之Sqli-labs下载安装重置数据库报错解决办法之一(#0{main}thrown in D:\Software\phpstudy_pro\WWW\sqli-labs-……)
- JVM -- class loading process and runtime data area
- Technical experts from large factories: common thinking models in architecture design
- In the era of low code development, is it still needed?
- 墨者学院-phpMyAdmin后台文件包含分析溯源
- Take you to master the formatter of visual studio code
猜你喜欢
Wechat has new functions, and the test is started again
节点基础~节点操作
Zephyr Learning note 2, Scheduling
User login function: simple but difficult
This monitoring system can monitor the turnover intention and fishing all, and the product page has 404 after the dispute appears
In the era of low code development, is it still needed?
Unity opens the explorer from the inspector interface, selects and records the file path
[kubernetes series] kubesphere is installed on kubernetes
线性代数1.1
[real case] how to deal with the failure of message consumption?
随机推荐
Used on windows Bat file startup project
Zephyr 学习笔记1,threads
深入浅出:了解时序数据库 InfluxDB
Oceanbase is the leader in the magic quadrant of China's database in 2021
Guoguo took you to write a linked list, and the primary school students said it was good after reading it
Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
猜数字游戏
Oracle-存储过程与函数
One of the general document service practice series
Xcode 14之大变化详细介绍
Implementation of ZABBIX agent active mode
如何用MOS管来实现电源防反接电路
[Chongqing Guangdong education] National Open University spring 2019 770 real estate appraisal reference questions
MySQL error resolution - error 1261 (01000): row 1 doesn't contain data for all columns
Would you like to go? Go! Don't hesitate if you like it
PCIe knowledge points -010: where to get PCIe hot plug data
A real penetration test
Detailed introduction to the big changes of Xcode 14
Activiti常见操作数据表关系
弈柯莱生物冲刺科创板:年营收3.3亿 弘晖基金与淡马锡是股东