当前位置:网站首页>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 !!!
边栏推荐
- 2. Nonlinear regression
- Know how to get the traffic password
- Su embedded training - C language programming practice (implementation of address book)
- Chapter XI feature selection
- New library online | information data of Chinese journalists
- 133. 克隆图
- Parade ps8625 | replace ps8625 | EDP to LVDS screen adapter or screen drive board
- Chapter VIII integrated learning
- Class head up rate detection based on face recognition
- Cross modal semantic association alignment retrieval - image text matching
猜你喜欢
5.过拟合,dropout,正则化
14. Draw network model structure
1. Linear regression
Get started quickly using the local testing tool postman
Chapter 7 Bayesian classifier
A network composed of three convolution layers completes the image classification task of cifar10 data set
Ag7120 and ag7220 explain the driving scheme of HDMI signal extension amplifier | ag7120 and ag7220 design HDMI signal extension amplifier circuit reference
Use "recombined netlist" to automatically activate eco "APR netlist"
130. Zones environnantes
Generic configuration legend
随机推荐
13.模型的保存和載入
jemter分布式
Codeforces Round #804 (Div. 2)
Ag9310 design USB type C to hdmi+u2+5v slow charging scheme design | ag9310 expansion dock scheme circuit | type-C dongle design data
Ag9310 for type-C docking station scheme circuit design method | ag9310 for type-C audio and video converter scheme circuit design reference
133. 克隆图
12.RNN应用于手写数字识别
11.递归神经网络RNN
How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
letcode43:字符串相乘
Markdown learning (entry level)
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
Basic types of 100 questions for basic grammar of Niuke
Basic implementation of pie chart
[deep learning] AI one click to change the sky
Saving and reading of network model
2. Nonlinear regression
[go record] start go language from scratch -- make an oscilloscope with go language (I) go language foundation
Use "recombined netlist" to automatically activate eco "APR netlist"