当前位置:网站首页>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 !!!
边栏推荐
- High quality USB sound card / audio chip sss1700 | sss1700 design 96 kHz 24 bit sampling rate USB headset microphone scheme | sss1700 Chinese design scheme explanation
- New library online | cnopendata China Star Hotel data
- Share a latex online editor | with latex common templates
- Jemter distributed
- Chapter IV decision tree
- Letcode43: string multiplication
- NVIDIA Jetson test installation yolox process record
- Smart agricultural technology framework
- Basic implementation of pie chart
- Vscode reading Notepad Chinese display garbled code
猜你喜欢

New library launched | cnopendata China Time-honored enterprise directory

General configuration tooltip

3. MNIST dataset classification

Letcode43: string multiplication

8.优化器

Class head up rate detection based on face recognition

jemter分布式

1. Linear regression

Led serial communication

Chapter 16 intensive learning
随机推荐
10. CNN applied to handwritten digit recognition
13. Enregistrement et chargement des modèles
国内首次,3位清华姚班本科生斩获STOC最佳学生论文奖
The whole life cycle of commodity design can be included in the scope of industrial Internet
Common effects of line chart
7. Regularization application
图像数据预处理
12. RNN is applied to handwritten digit recognition
3.MNIST数据集分类
General configuration toolbox
Malware detection method based on convolutional neural network
Ag9310 same function alternative | cs5261 replaces ag9310type-c to HDMI single switch screen alternative | low BOM replaces ag9310 design
Led serial communication
1.线性回归
Know how to get the traffic password
Study notes of single chip microcomputer and embedded system
14.绘制网络模型结构
14. Draw network model structure
For the first time in China, three Tsinghua Yaoban undergraduates won the stoc best student thesis award
基础篇——整合第三方技术