当前位置:网站首页>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 !!!
边栏推荐
- Complete model verification (test, demo) routine
- How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
- STL -- common function replication of string class
- Ag9310 same function alternative | cs5261 replaces ag9310type-c to HDMI single switch screen alternative | low BOM replaces ag9310 design
- Know how to get the traffic password
- How to transfer Netease cloud music /qq music to Apple Music
- Ag9310 for type-C docking station scheme circuit design method | ag9310 for type-C audio and video converter scheme circuit design reference
- My best game based on wechat applet development
- C# ?,?.,?? .....
- C# ?,?.,?? .....
猜你喜欢

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

11. Recurrent neural network RNN

Password recovery vulnerability of foreign public testing

13.模型的保存和載入

3. MNIST dataset classification

Cross modal semantic association alignment retrieval - image text matching

10.CNN应用于手写数字识别

13. Enregistrement et chargement des modèles

Definition and classification of energy

2022-07-07: the original array is a monotonic array with numbers greater than 0 and less than or equal to K. there may be equal numbers in it, and the overall trend is increasing. However, the number
随机推荐
[deep learning] AI one click to change the sky
Ag9310meq ag9310mfq angle two USB type C to HDMI audio and video data conversion function chips parameter difference and design circuit reference
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
AI zhetianchuan ml novice decision tree
Chapter IV decision tree
Chapter 5 neural network
7.正则化应用
C# ?,?.,?? .....
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
13. Model saving and loading
NTT template for Tourism
Basic implementation of pie chart
2.非线性回归
Capstone/cs5210 chip | cs5210 design scheme | cs5210 design data
EDP to LVDS conversion design circuit | EDP to LVDS adapter board circuit | capstone/cs5211 chip circuit schematic reference
新库上线 | CnOpenData中国星级酒店数据
Class head up rate detection based on face recognition
C#中string用法
STL -- common function replication of string class
The whole life cycle of commodity design can be included in the scope of industrial Internet