当前位置:网站首页>[leetcode] merge K ascending linked lists
[leetcode] merge K ascending linked lists
2022-06-11 01:45:00 【xiaoshijiu333】
#LeetCode A daily topic 【 Special topic of linked list 】
- Merge K An ascending list
https://leetcode-cn.com/problems/merge-k-sorted-lists/ - analysis
Merge two ascending lists , It has been implemented many times before ; Now merge K An ascending list , Here's an array of linked lists , Merge into an orderly ;
The easiest idea to think of is : A cycle , Record the results in pairs - Achieve one
// Merge K An ordered list
// loop 、 Split into merge 2 There is a sequence table
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
}
ListNode dummy = null, node = null;
for (ListNode list : lists) {
node = mergeTwoList(dummy, list);
dummy = node;
}
return node;
}
private ListNode mergeTwoList(ListNode list1, ListNode list2) {
ListNode result = new ListNode(0);
ListNode temp = result;
ListNode node1 = list1, node2 = list2;
while (node1 != null && node2 != null) {
if (node1.val < node2.val) {
temp.next = node1;
node1 = node1.next;
} else {
temp.next = node2;
node2 = node2.next;
}
temp = temp.next;
}
if (node1 != null) {
temp.next = node1;
}
if (node2 != null) {
temp.next = node2;
}
return result.next;
}
LeetCode Time consuming :101ms
- Achieve two
Divide and conquer : Divide and rule , Constantly split in half , Final re merger ;
This is also the idea of merging and sorting
// Divide and conquer : Divide and rule , Constantly split in half , Final re merger
// This is also the idea of merging and sorting
public ListNode mergeKLists02(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
}
return mergeTwoList02(lists, 0, lists.length - 1);
}
// recursive : Don't fall into the trap of following him round by round ;
// Recursive Trilogy :
// 1. What are the closing conditions
// 2. What is the return value
// 3. What does this level recursion need to do
/* For example, every recursion : 1、 The end condition : Split to only one node , end ; Right here left=right, Marked the same position of the array 2、 What is the return value : We need to handle the linked list 3、 What recursion at this level should do : This level recursion , We have the split linked list on the left 、 The split linked list on the right , Then we can merge two linked lists */
private ListNode mergeTwoList02(ListNode[] lists, int left, int right) {
if (left == right) {
return lists[left];
}
int mid = (left + right) / 2;
ListNode nodeLeft = mergeTwoList02(lists, left, mid);
ListNode nodeRight = mergeTwoList02(lists, mid + 1, right);
return mergeTwoList(nodeLeft, nodeRight);
}
private ListNode mergeTwoList(ListNode list1, ListNode list2) {
ListNode result = new ListNode(0);
ListNode temp = result;
ListNode node1 = list1, node2 = list2;
while (node1 != null && node2 != null) {
if (node1.val < node2.val) {
temp.next = node1;
node1 = node1.next;
} else {
temp.next = node2;
node2 = node2.next;
}
temp = temp.next;
}
if (node1 != null) {
temp.next = node1;
}
if (node2 != null) {
temp.next = node2;
}
return result.next;
}
LeetCode Time consuming :1ms
fast 100 times : Because use the result to merge the next , The linked list to be processed will be longer and longer ;
Divide and conquer method , Continuously split into two , A merger of two , Then go back and merge the two , Much less data than before
- summary
Recursive routine :https://lyl0724.github.io/2020/01/25/1/
The recursion routine summarized in this article —— Trilogy , I feel very clear after reading- What are the closing conditions
- What is the return value
- What does this level recursion need to do
I hope you can directly answer similar questions later
边栏推荐
- ROS参数服务器
- 2022.6.6-----leetcode. seven hundred and thirty-two
- Role of handlermethodargumentresolver + use case
- SAS cluster analysis (system cluster, dynamic cluster fastclus, variable cluster varclus)
- Inventory management and strategy mode
- [VBA Script] extract the information and pending status of all annotations in the word document
- 腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
- 卡尔曼滤波(KF)、拓展卡尔曼滤波(EKF)推导
- Conda安装Pytorch后numpy出现问题
- [ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design
猜你喜欢

如何下载网页照片

1.4px4 program download

2.1 ros+px4 simulation - Fixed Point flight control

How to use user-defined annotation for parameter verification

Web3 ecological decentralized financial platform sealem Finance

QGC地面站使用教程

对象存储 S3 在分布式文件系统中的应用

Leetcode linked list queue stack problem

From "0" to "tens of millions" concurrency, 14 technological innovations of Alibaba distributed architecture

2.2. Ros+px4 simulation multi-point cruise flight - Square
随机推荐
Project_ Visual analysis of epidemic data based on Web Crawler
Multipartfile and file interoperation tool classes
[leetcode] reverse linked list
2021-2-26编程语言知识点整理
detectron2训练自己的数据集和转coco格式
MATLAB数组其他常见操作笔记
"It looks like robbing tickets but actually robbing money". Don't be fooled by fancy ticket robbing products again and again
2021-3-1MATLAB写cnn的mnist数据库训练
LeetCode 1010 Pairs of Songs With Total Durations Divisible by 60 (hash)
What is the C-end and what is the b-end? Let me tell you
Leetcode 2054 two best non overlapping events
Threejs: how to get the boundingbox of geometry?
Middleware_ Redis_ 05_ Persistence of redis
ros缺包怎么办
kubernetes 二进制安装(v1.20.15)(七)加塞一个工作节点
Leetcode 1094 car pooling (Analog)
Hao expresses his opinions: what small good habits have you adhered to?
2022.6.6-----leetcode.732
1.5、PX4载具选择
[path planning] week 1: Path Planning open source code summary (ROS) version