当前位置:网站首页>[leetcode ladder] linked list · 021 merge two ordered linked lists
[leetcode ladder] linked list · 021 merge two ordered linked lists
2022-07-25 21:41:00 【kikokingzz】

Title Description :
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 1:

Input :l1 = [1,2,4], l2 = [1,3,4]
Output :[1,1,2,3,4,4]Example 2:
Input :l1 = [], l2 = []
Output :[]Example 3:
Input :l1 = [], l2 = [0]
Output :[0]Topic link :
Their thinking :
Conventional method 1: Create a new linked list to receive
I think this method should be the most basic approach , Use four pointers to operate , Two pointers are used to traverse the original linked list , A pointer is used to identify the header of the new linked list , Another pointer is used to identify the end of the new linked list , Facilitate the tail insertion of new nodes .
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){ struct ListNode*n1=list1; //n1 Pointers are used to traverse list1 struct ListNode*n2=list2; //n2 Pointers are used to traverse list2 struct ListNode*newHead=NULL; struct ListNode*tail=NULL; while(n1&&n2) // When n1 or n2 There is one for NULL Jump out of the loop { if(n1->val<=n2->val) // When n1 The value of the node pointed to is less than or equal to n2 after { if(newHead==NULL) // When the new linked list is empty { newHead=n1; // take n1 As the head node of the new linked list tail=n1; n1=n1->next; } else // When the new linked list is not empty { tail->next=n1; // Connect the tail node of the new linked list to n1 tail=n1; n1=n1->next; } } else // When n1 The node value pointed to is greater than n2 when { if(newHead==NULL) // When the new linked list is empty { newHead=n2; // take n2 As the head node of the new linked list tail=n2; n2=n2->next; } else // When the new linked list is not empty { tail->next=n2; // Connect the tail node of the new linked list to n1 tail=n2; n2=n2->next; } } } if(newHead==NULL) // It shows that a linked list is empty at the beginning { if(n1==NULL) return n2; else if(n2==NULL) return n1; else if(n1==NULL&&n2==NULL) return NULL; } if(n1==NULL) tail->next=n2; if(n2==NULL) tail->next=n1; return newHead; }
To sum up : We found that this problem is slightly longer , It is because we need to discuss that the new chain header pointer may or may not be empty , For the problem that the head node of the new linked list may be empty , We must be here 【 Remove linked list elements 】 It has been introduced in this sentence , Using sentinel method can greatly simplify the above code .
Conventional method 2: Sentinel law
To simplify the above operation , In addition to establishing a sentry post , In fact, we only need to set two pointers ,n1 and n2 The pointer can use the original list1 and list2 To replace , Because of the result of this problem, we don't need to return to the original linked list , Just return a new merged linked list .
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){ struct ListNode* newHead=NULL,*tail=NULL; newHead=tail=(struct ListNode*)malloc(sizeof(struct ListNode));// At first tail and newHead Point to the same dynamic node space while(list1&&list2) // When list1 or list2 There is a null jump out of the loop { if(list1->val<=list2->val) { if(tail==NULL) // When the new linked list is empty { newHead->next=tail=list1; // take list1 The node pointed to is linked after the sentinel } else // When the new linked list is not empty { tail->next=list1; // take list1 The node pointed to is directly linked after the new linked list tail=list1; } list1=list1->next; //list1 Take a step back } else { if(tail==NULL) { newHead->next=tail=list2; } else { tail->next=list2; tail=list2; } list2=list2->next; } } if(list1==NULL) tail->next=list2; if(list2==NULL) tail->next=list1; struct ListNode* list=newHead->next; // Define a list The pointer points after the sentinel node free(newHead); //free Drop the sentinel node return list; }
To sum up : The cleverness of using sentinel method here is that it will tail and newHead All point to the same sentinel node space . For example, when there is a list It's empty time , Cannot enter at this time while loop , But you can enter with while The program that jumps out after the loop shares the same piece of code , Without classified discussion , This is because at first tail and newHead It points to the same sentinel node space , So for those programs that don't enter the loop , Their execution tail->next=list1, It's equivalent to executing newHead->next=list1. Eventually they and those enter while The procedure of the loop is the same , Finally, share one return newHead->next Statement can realize output operation .
边栏推荐
- As a test, how to understand thread synchronization and asynchrony
- Byte side: can TCP and UDP use the same port?
- 【Redis底层解析】字符串类型
- Job interviews are always a second kill? After reading the seckill system notes secretly stored by JD T8, I have given my knees
- Simple use of protobuf
- resize函数的作用「建议收藏」
- 919. Complete binary tree inserter: simple BFS application problem
- es6--解构赋值
- How to evaluate hardware resources (number of CPUs, memory size) when Oracle migrates from small computers to x86 architecture? Is there a measurement index or company?
- Vivo official website app full model UI adaptation scheme
猜你喜欢

My heart's broken! After being cheated by 30000, a 16-year-old girl was unconvinced and cheated by 50000
![[database] conceptual design, logical design, relational database design theory](/img/4d/be7ab21c98fc1bf4b63e4abe22d9fc.png)
[database] conceptual design, logical design, relational database design theory

立创EDA——我为什么要学EDA
![[ManageEngine] value brought by Siem to enterprises](/img/1e/56d64d193e0428523418bef5e98866.png)
[ManageEngine] value brought by Siem to enterprises

FAW red flag "King fried" is listed, which is safe and comfortable

Ijcai2022 meeting! Microsoft and other tutorials on domain generalization
![[manageengine]itsm application in retail industry](/img/25/e8d9a320c5d4b1cf2e187b61180991.png)
[manageengine]itsm application in retail industry

Oxford University: many common insomnia drugs lack long-term safety data
![[database] index](/img/57/4921cf3eee9e8395415a8624b28d0a.png)
[database] index

How to implement distributed locks with redis?
随机推荐
I'm also drunk. Eureka delayed registration and this pit!
Kali modify the update source (it is not safe to update with this source)
Database SQL statement exercise "suggestions collection"
mysql8.0 mha实现高可用《mha》
Detailed explanation of several ideas for implementing timed tasks in PHP
How to choose sentinel vs. hystrix current limiting?
【Redis底层解析】链表类型
Pyg tutorial (8): calculate a more efficient sparse matrix form
zigbee物联网开发平台(工业物联网)
【面试:并发篇25:多线程:volatile】可见性
Naming rules for BSP of Quanzhi chip
Intel base instruction -- bnd
The adequacy of source evaluation forum · observation model test
In depth understanding of seven specific ways to enhance code scalability
Oxford University: many common insomnia drugs lack long-term safety data
NPM module removal_ [solved] after NPM uninstalls the module, the module is not removed from package.json [easy to understand]
狗粮的成分
919. Complete binary tree inserter: simple BFS application problem
2022 latest examination questions and answers of eight members (standard staff) of Shanghai Architecture
Fastjson deserialization vulnerability utilization analysis collection
https://leetcode.cn/problems/merge-two-sorted-lists/

