当前位置:网站首页>Leetcode daily practice - 21. Merge two ordered linked lists
Leetcode daily practice - 21. Merge two ordered linked lists
2022-07-27 21:07:00 【An airliner flying to the stars】
Preface
Wassup guys! I am a Edison
It's today LeetCode Upper leetcode 21. Merge two ordered lists
Let’s get it!

List of articles
1. Topic analysis
Merge two ascending linked lists into a new Ascending Link list and return . The new linked list is made up of all the nodes of the given two linked lists .
Example 1:
Example 2:
Example 3:
2. Title diagram
This question is actually Merger Ideas , Take the smaller node each time Tail insertion To the new linked list
Train of thought
Define pointer list 1 Point to the first linked list Head node , Define pointer list 2 Point to the second linked list Head node ;
Define a new linked list , At this point, the head node head and Tail node tail All for NULL, As shown in the figure 
Let's analyze Tail insertion The process of :
The first 1 Time , When list 1 be equal to list 2 when , Just put list 1 Insert into the new linked list , to head and tail;
The first 2 Time , When list 2 Less than list 1 when , Just put list 2 Insert into the new linked list , to update tail;
The first 3 Time , When list 1 Less than list 2 when , Just put list 1 Insert into the new linked list , to update tail;
Here you can see the dynamic diagram demonstration directly 
Be careful :
When any one of the linked lists points to NULL when , Put the remaining elements of the linked list directly Copy To the new linked list .
such as ,list 1 Point first NULL, So directly put list 2 Get the remaining elements New linked list in , There is no need to update tail 了 , Directly return to the new linked list Head node .
Why not update tail Well ?
What we are going back to is Head node , There is no need to return to the new Tail node , What is defined here tail Just convenient Tail insertion .
Interface code
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
if (list1 == NULL) {
return list2;
}
if (list2 == NULL) {
return list1;
}
struct ListNode* head = NULL, *tail = NULL;
while (list1 && list2) {
// list1 Less than list2
if ((list1->val) < (list2->val)) {
if (tail == NULL) {
head = tail = list1;
}
else {
tail->next = list1;
tail = list1;
// tail = tail->next
}
list1 = list1->next;
}
// list1 Greater than or equal to list2
else {
if (tail == NULL) {
head = tail = list2;
}
else {
tail->next = list2;
tail = list2;
// tail = tail->next
}
list2 = list2->next;
}
}
if (list1) {
tail->next = list1;
}
if (list2) {
tail->next = list2;
}
return head;
}
Submit results 
Train of thought
We can build a new Leading node New linked list of , That is to say Sentry post The head node of , This node does not store valid data .
So this Take the lead and Don't take the lead What's the difference ?
stay Train of thought in , The new linked list we defined does not take the lead ,head and tail All point to NULL, So at insertion , To judge ,tail Is it NULL The situation of .
So if I set one Sentry post The head node of , Then there is no need to judge , Even if there is no value at this time ,head and tail It's impossible for empty , So it's going on Tail insertion when , Insert the element directly into Sentry post Just behind .
Principle or and Train of thought It's the same , So look directly at the motion chart 
Be careful :
On return , Can't return head, It's going back to head Point to the next, That is to say Sentry post The next node of ;
Because the two linked lists given in the title do not have sentinel positions , So after our merger , Those who return are also without sentry positions
Interface code
// Single linked list of leading nodes
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
struct ListNode* head = NULL, *tail = NULL;
// Set up a sentry post
head = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
head->next = NULL;
while (list1 && list2) {
if ((list1->val) < (list2->val)) {
tail->next = list1;
tail = list1;
list1 = list1->next;
}
else {
tail->next = list2;
tail = list2;
list2 = list2->next;
}
}
if (list1) {
tail->next = list1;
}
if (list2) {
tail->next = list2;
}
// Release
struct ListNode* list = head->next;
free(head);
return list;
}
Submit results 
边栏推荐
- Where is the program?
- R语言dplyr包进行数据分组聚合统计变换(Aggregating transforms)、计算dataframe数据的分组加和值(sum)
- JS closure knowledge
- Hexagon_V65_Programmers_Reference_Manual(5)
- 搭建discuz论坛并攻破盗取数据库
- Diffuse reflection of QT OpenGL light
- [numpy] array index and slice
- Programmer growth Chapter 18: project launch
- 五大知名人士对于AI的忧虑
- 基于ATX自动化测试解决方案
猜你喜欢

Automated testing ----- selenium (II)

The variable "lattice" or class "lattice.latticeeasy" (matlab) is not defined

How to make personalized recommendations instantly accessible? Cloud native database gaussdb (for redis) to help

Leetcode-209- subarray with the smallest length

如何对话CIO/CTO
![[Numpy] 数组索引和切片](/img/ce/34db7aef3fefe8a03e638d0838492f.png)
[Numpy] 数组索引和切片

Obtain website shell permission based on file upload vulnerability

如何查看蓝牙耳机的蓝牙版本

Source Insight 4.0使用介绍

LeetCode每日一练 —— 链表中倒数第 k 个结点
随机推荐
Hexagon_ V65_ Programmers_ Reference_ Manual(8)
Recommend a powerful search tool listary
Qt 链接MSSQL
How does the industrial switch enter the web management interface?
QT OpenGL makes objects move under care to form animation
[deep learning] pytoch torch Autograd automatic differential engine
Diffuse reflection of QT OpenGL light
VI working mode (3 kinds) and mode switching (conversion)
MySQL design optimization generates columns
自动化测试----unittest框架
一种比读写锁更快的锁,还不赶紧认识一下
重复的DNA序列[hash判定重复+滑动窗口+二进制编码之位运算]
[Numpy] 数组索引和切片
opencv实现图片裁剪和缩放
CPDA | how to have data analysis thinking?
怎样实现文档协同?
QT link MSSQL
R语言dplyr包进行数据分组聚合统计变换(Aggregating transforms)、计算dataframe数据的分组加和值(sum)
R语言使用epiDisplay包的lroc函数可视化logistic回归模型的ROC曲线并输出诊断表(diagnostic table)、可视化多条ROC曲线、使用legend函数为可视化图像添加图例
Sscanf caused the address to be out of bounds