当前位置:网站首页>Leetcode notes No.21
Leetcode notes No.21
2022-07-08 01:13:00 【__ Small crisp__】
leetcode note No.21
21. Merge two ordered lists
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 :
Input :1->2->4, 1->3->4
Output :1->1->2->3->4->4
The data type is defined in the title :
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */
analysis
My idea is :
Create a head node as a new starting point , from l1 and l2 The heads of the two ordered linked lists begin to compare , Because the requirements are in ascending order , So the smaller one is assigned to the head node first next, Then the pointer moves back
Answer key
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
struct ListNode* o = (struct ListNode*)malloc(sizeof(struct ListNode));
o->next = NULL;
struct ListNode* temp = o;
while(l1 != NULL && l2 != NULL) {
if (l1->val < l2->val) {
o->next = l1;
o = o->next;
l1 = l1->next;
}
else {
o->next = l2;
o = o->next;
l2 = l2->next;
}
}
if (l1 != NULL && l2 == NULL) {
o->next = l1;
}
else if (l1 == NULL && l2 != NULL) {
o->next = l2;
}
o = temp->next;
free(temp);
return o;
}
Be careful
o->next = NULL;
I didn't write this code at first ( Not fully considered ). stay l1 and l2 When both linked lists are empty , Neither the cycle nor the following judgment will go to , Not writing this code means o->next It's a random address ( because malloc It will not automatically help us initialize the requested memory space ), This address is not governed by this program , So it belongs to illegal access . We return this random address , If it's just reading, it won't cause any serious problems , If you write values inside , Then the consequences deserve attention .
summary
Remember to initialize the address before using !!!
Remember to initialize the address before using !!!
Remember to initialize the address before using !!!
边栏推荐
- C# ?,?.,?? .....
- Serial port receives a packet of data
- 串口接收一包数据
- Overall introduction of the project
- Know how to get the traffic password
- How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
- [note] common combined filter circuit
- How to write mark down on vscode
- German prime minister says Ukraine will not receive "NATO style" security guarantee
- Invalid V-for traversal element style
猜你喜欢

133. Clone map

Chapter 16 intensive learning

国内首次,3位清华姚班本科生斩获STOC最佳学生论文奖

Design method and reference circuit of type C to hdmi+ PD + BB + usb3.1 hub (rj45/cf/tf/ sd/ multi port usb3.1 type-A) multifunctional expansion dock

Ag9310 design USB type C to hdmi+u2+5v slow charging scheme design | ag9310 expansion dock scheme circuit | type-C dongle design data

Smart grid overview

7.正则化应用

Fofa attack and defense challenge record

6. Dropout application

AI zhetianchuan ml novice decision tree
随机推荐
Ag9310 same function alternative | cs5261 replaces ag9310type-c to HDMI single switch screen alternative | low BOM replaces ag9310 design
新库上线 | CnOpenData中国星级酒店数据
8. Optimizer
AI遮天传 ML-回归分析入门
Codeforces Round #804 (Div. 2)
130. Surrounding area
General configuration tooltip
How to write mark down on vscode
NVIDIA Jetson test installation yolox process record
Design method and application of ag9311maq and ag9311mcq in USB type-C docking station or converter
EDP to LVDS conversion design circuit | EDP to LVDS adapter board circuit | capstone/cs5211 chip circuit schematic reference
Implementation of adjacency table of SQLite database storage directory structure 2-construction of directory tree
【深度学习】AI一键换天
Ag9311maq design 100W USB type C docking station data | ag9311maq is used for 100W USB type C to HDMI with PD fast charging +u3+sd/cf docking station scheme description
Basic types of 100 questions for basic grammar of Niuke
10. CNN applied to handwritten digit recognition
14.绘制网络模型结构
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
13.模型的保存和载入
Swift get URL parameters