当前位置:网站首页>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
边栏推荐
- This monitoring system can monitor the turnover intention and fishing all, and the product page has 404 after the dispute appears
- Xcode 14之大变化详细介绍
- OKR vs. KPI figure out these two concepts at once!
- Thesis learning -- time series similarity query method based on extreme point characteristics
- [kubernetes series] kubesphere is installed on kubernetes
- Flask 常用组件
- 【性能测试】一文读懂Jmeter
- Easy to understand: understand the time series database incluxdb
- Unity 从Inspector界面打开资源管理器选择并记录文件路径
- ZABBIX monitoring system custom monitoring content
猜你喜欢
[kubernetes series] kubesphere is installed on kubernetes
ZABBIX monitoring system custom monitoring content
Project 1 household accounting software (goal + demand description + code explanation + basic fund and revenue and expenditure details record + realization of keyboard access)
Linear algebra 1.1
It's healthy to drink medicinal wine like this. Are you drinking it right
[network security] what is emergency response? What indicators should you pay attention to in emergency response?
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Introduction to sap commerce cloud B2B organization function
NPM run build error
How to send mail with Jianmu Ci
随机推荐
人生规划(Flag)
Zephyr 學習筆記2,Scheduling
Activiti常見操作數據錶關系
How to reset IntelliSense in vs Code- How to reset intellisense in VS Code?
21个战略性目标实例,推动你的公司快速发展
Zephyr Learning note 2, Scheduling
MySQL中的文本處理函數整理,收藏速查
Go h*ck yourself:online reconnaissance (online reconnaissance)
Guoguo took you to write a linked list, and the primary school students said it was good after reading it
Jianmu continuous integration platform v2.2.2 release
谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展
Wechat has new functions, and the test is started again
Introduction to neural network (Part 2)
博客停更声明
Email alarm configuration of ZABBIX monitoring system
Oracle-存储过程与函数
Go learning notes - constants
rapidjson读写json文件
[untitled] notice on holding "2022 traditional fermented food and modern brewing technology"
节点基础~节点操作