当前位置:网站首页>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 
边栏推荐
- NATAPP内网穿透工具外网访问个人项目
- redis cook book.notes.
- Obtain website shell permission based on file upload vulnerability
- Recommend a powerful search tool listary
- SRE相关问题答疑
- 北京/上海/广州/深圳DAMA-CDGA/CDGP数据治理认证报名条件
- 【R语言】【1】初学R语言语法使用Rstudio编辑
- Hexagon_V65_Programmers_Reference_Manual(6)
- Beijing / Shanghai / Guangzhou / Shenzhen dama-cdga/cdgp data governance certification registration conditions
- Introduction to source insight 4.0
猜你喜欢

Hexagon_ V65_ Programmers_ Reference_ Manual(5)

自动化测试----selenium(二)

中地数码:融合创新国产GIS 乘风而上助推实景三维中国建设

knife4j通过js动态刷新全局参数

hcip第五天

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

Sscanf caused the address to be out of bounds

IOU 目标跟踪其二:VIOU Tracker

Introduction to rk3399 platform introduction to proficient series (Introduction) 21 day learning challenge
![[dart] a programming language for cross end development](/img/e1/1167a322bb9f276f2e00fb12414d17.png)
[dart] a programming language for cross end development
随机推荐
Brief description of tenant and multi tenant concepts in cloud management platform
Riding lantern case
redis cook book.notes.
北京/上海/广州/深圳DAMA-CDGA/CDGP数据治理认证报名条件
go --- air自动重新编译
[numpy] array index and slice
Face recognition 5.1- insightface face face detection model training practice notes
Read Plato & nbsp; Eplato of farm and the reasons for its high premium
Automated testing ----- selenium (II)
LeetCode每日一练 —— 203. 移除链表元素
[today in history] July 27: model testing pioneer was born; Microsoft acquires qdos; The first laser typesetting Chinese newspaper
R语言使用dplyr包左连接两个dataframe数据(left join)
实名认证在文旅出行行业的应用场景有哪些?
How to realize document collaboration?
Source Insight 4.0使用介绍
Tips for file upload to bypass WAF
14天鸿蒙设备开发实战-第七章 设备联网上云 学习笔记
How to translate the address in the program?
原生对象、内置对象、宿主对象的区别
Hexagon_ V65_ Programmers_ Reference_ Manual(8)