当前位置:网站首页>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 !!!
边栏推荐
- 国内首次,3位清华姚班本科生斩获STOC最佳学生论文奖
- Complete model training routine
- Study notes of single chip microcomputer and embedded system
- 10. CNN applied to handwritten digit recognition
- A speed Limited large file transmission tool for every major network disk
- 50Mhz产生时间
- AI zhetianchuan ml novice decision tree
- Jemter distributed
- 5.过拟合,dropout,正则化
- Ag9310 same function alternative | cs5261 replaces ag9310type-c to HDMI single switch screen alternative | low BOM replaces ag9310 design
猜你喜欢

Taiwan Xinchuang sss1700 latest Chinese specification | sss1700 latest Chinese specification | sss1700datasheet Chinese explanation

Get started quickly using the local testing tool postman

Generic configuration legend

General configuration title

From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run

Micro rabbit gets a field of API interface JSON

2.非线性回归

Ag9310 same function alternative | cs5261 replaces ag9310type-c to HDMI single switch screen alternative | low BOM replaces ag9310 design

General configuration tooltip

1.线性回归
随机推荐
130. Surrounding area
4.交叉熵
[note] common combined filter circuit
国内首次,3位清华姚班本科生斩获STOC最佳学生论文奖
Introduction to ML regression analysis of AI zhetianchuan
50Mhz产生时间
letcode43:字符串相乘
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
Chapter 7 Bayesian classifier
Chapter IV decision tree
Taiwan Xinchuang sss1700 latest Chinese specification | sss1700 latest Chinese specification | sss1700datasheet Chinese explanation
【深度学习】AI一键换天
Jemter distributed
Su embedded training - Day6
1.线性回归
Ag9310meq ag9310mfq angle two USB type C to HDMI audio and video data conversion function chips parameter difference and design circuit reference
How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
New library online | information data of Chinese journalists
Cs5212an design display to VGA HD adapter products | display to VGA Hd 1080p adapter products
USB type-C mobile phone projection scheme | USB type-C docking station scheme | TV / projector type-C converter scheme | ag9300ag9310ag9320