当前位置:网站首页>[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
边栏推荐
- Clean up the broken artifacts data (.lastUpdated files) and reload the project. Problem resolution
- 2021-2-26编程语言知识点整理
- Summary of SAS final review knowledge points (notes on Application of multivariate statistics experiment)
- Set up a flag -- Reconstruct promise
- 1.5、PX4载具选择
- ros缺包怎么办
- Px4 from abandonment to mastery (twenty four): customized model
- 记录打包GoogleChrome浏览器插件
- [leetcode] reverse linked list II
- Leetcode 698 partition to K equal sum subsets (DFS pruning)
猜你喜欢

What is the C-end and what is the b-end? Let me tell you

Sealem finance builds Web3 decentralized financial platform infrastructure

項目_基於網絡爬蟲的疫情數據可視化分析

Current limiting and download interface request number control

Function of barcode fixed assets management system, barcode management of fixed assets

Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack

1.3 introduction to ROS UAV

1.5 Px4 vehicle selection

关于概率统计中的排列组合

1.3 ROS 无人机简介
随机推荐
threejs:如何获得几何体的boundingBox?
PX4从放弃到精通(二十四):自定义机型
ROS parameter server
SAS principal component analysis (finding correlation matrix, eigenvalue, unit eigenvector, principal component expression, contribution rate and cumulative contribution rate, and data interpretation)
卡尔曼滤波(KF)、拓展卡尔曼滤波(EKF)推导
Px4 from abandonment to mastery (twenty four): customized model
Detailed explanation of classic papers on OCR character recognition
Project_ Visual analysis of epidemic data based on Web Crawler
Application of object storage S3 in distributed file system
The emperors of the Ming Dynasty
Leetcode permutation and combination problem backtracking
1.7 calibration of Px4 remote controller
MATLAB数字运算函数笔记
Detectron2 trains its own dataset and converts it to coco format
Leetcode 1574 shortest subarray to be removed to make array sorted
1.4PX4程序下载
EXJ形儿多眼前因断会满意接MBtXE
[path planning] week 1: hodgepodge
1.2、ROS+PX4预备基础知识
Be careful, these hidden "bugs" of "array" to "collection"“