当前位置:网站首页>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 !!!
边栏推荐
- 手机上炒股安全么?
- 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
- 5.过拟合,dropout,正则化
- Binder core API
- My best game based on wechat applet development
- Common effects of line chart
- C#中string用法
- Su embedded training - Day9
- Basic types of 100 questions for basic grammar of Niuke
- Fundamentals - integrating third-party technology
猜你喜欢

英雄联盟胜负预测--简易肯德基上校

新库上线 | CnOpenData中华老字号企业名录

网络模型的保存与读取
![[necessary for R & D personnel] how to make your own dataset and display it.](/img/50/3d826186b563069fd8d433e8feefc4.png)
[necessary for R & D personnel] how to make your own dataset and display it.

5.过拟合,dropout,正则化

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

Analysis of 8 classic C language pointer written test questions

A network composed of three convolution layers completes the image classification task of cifar10 data set

130. 被圍繞的區域

New library launched | cnopendata China Time-honored enterprise directory
随机推荐
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
Prediction of the victory or defeat of the League of heroes -- simple KFC Colonel
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
Fundamentals - integrating third-party technology
A speed Limited large file transmission tool for every major network disk
HDMI to VGA acquisition HD adapter scheme | HDMI to VGA 1080p audio and video converter scheme | cs5210 scheme design explanation
AI遮天传 ML-初识决策树
Study notes of single chip microcomputer and embedded system
swift获取url参数
2.非线性回归
The weight of the product page of the second level classification is low. What if it is not included?
[go record] start go language from scratch -- make an oscilloscope with go language (I) go language foundation
Codeforces Round #804 (Div. 2)
利用GPU训练网络模型
跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
Su embedded training - Day7
Analysis of 8 classic C language pointer written test questions
12.RNN应用于手写数字识别
4.交叉熵
EDP to LVDS conversion design circuit | EDP to LVDS adapter board circuit | capstone/cs5211 chip circuit schematic reference