当前位置:网站首页>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 !!!
边栏推荐
- Chapter XI feature selection
- 12. RNN is applied to handwritten digit recognition
- Capstone/cs5210 chip | cs5210 design scheme | cs5210 design data
- Redis, do you understand the list
- Analysis of 8 classic C language pointer written test questions
- 50Mhz产生时间
- My best game based on wechat applet development
- NVIDIA Jetson test installation yolox process record
- 130. 被圍繞的區域
- Led serial communication
猜你喜欢
130. Zones environnantes
Ag9310 for type-C docking station scheme circuit design method | ag9310 for type-C audio and video converter scheme circuit design reference
10.CNN应用于手写数字识别
Smart grid overview
解决报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
What does interface testing test?
Vscode is added to the right-click function menu
Chapter 5 neural network
完整的模型训练套路
Use "recombined netlist" to automatically activate eco "APR netlist"
随机推荐
Letcode43: string multiplication
Two methods for full screen adaptation of background pictures, background size: cover; Or (background size: 100% 100%;)
Taiwan Xinchuang sss1700 latest Chinese specification | sss1700 latest Chinese specification | sss1700datasheet Chinese explanation
14. Draw network model structure
Generic configuration legend
AI zhetianchuan ml novice decision tree
New library launched | cnopendata China Time-honored enterprise directory
4. Cross entropy
Chapter XI feature selection
【深度学习】AI一键换天
STL--String类的常用功能复写
Swift get URL parameters
Is it safe to speculate in stocks on mobile phones?
7.正则化应用
AI遮天传 ML-初识决策树
Complete model verification (test, demo) routine
Semantic segmentation model base segmentation_ models_ Detailed introduction to pytorch
10. CNN applied to handwritten digit recognition
Basic types of 100 questions for basic grammar of Niuke
Class head up rate detection based on face recognition