当前位置:网站首页>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 !!!
边栏推荐
- My best game based on wechat applet development
- General configuration toolbox
- NTT template for Tourism
- 4. Cross entropy
- 133. Clone map
- Invalid V-for traversal element style
- 50MHz generation time
- Parade ps8625 | replace ps8625 | EDP to LVDS screen adapter or screen drive board
- 6.Dropout应用
- Smart agricultural technology framework
猜你喜欢
图像数据预处理
2.非线性回归
Saving and reading of network model
利用GPU训练网络模型
Generic configuration legend
How to write mark down on vscode
Several frequently used OCR document scanning tools | no watermark | avoid IQ tax
Chapter XI feature selection
Cross modal semantic association alignment retrieval - image text matching
Analysis of 8 classic C language pointer written test questions
随机推荐
How to transfer Netease cloud music /qq music to Apple Music
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
Smart agricultural technology framework
130. 被围绕的区域
Scheme selection and scheme design of multifunctional docking station for type C to VGA HDMI audio and video launched by ange in Taiwan | scheme selection and scheme explanation of usb-c to VGA HDMI c
Basic types of 100 questions for basic grammar of Niuke
Swift get URL parameters
Parade ps8625 | replace ps8625 | EDP to LVDS screen adapter or screen drive board
Chapter XI feature selection
Several frequently used OCR document scanning tools | no watermark | avoid IQ tax
Design method and application of ag9311maq and ag9311mcq in USB type-C docking station or converter
Recommend a document management tool mendely Reference Manager
Vscode is added to the right-click function menu
英雄联盟胜负预测--简易肯德基上校
Basic realization of line chart (II)
Generic configuration legend
Y59. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (III, II)
国内首次,3位清华姚班本科生斩获STOC最佳学生论文奖
Malware detection method based on convolutional neural network
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