In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how Linked List-Easy merges the two sorted linked lists". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how Linked List-Easy merges the two sorted linked lists".
Merge two sorted linked lists and return a new linked list, which is also sorted.
Ideas for solving the problem:
Create two linked lists, one responsible for saving the header node and the other for recording the results of the comparison.
Language: c
/ * Definition for singly-linked list. * struct ListNode {* int val; * struct ListNode* next; *}; * / struct ListNode* mergeTwoLists (struct ListNode* L1, struct ListNode* 12) {struct ListNode* newlist = (struct ListNode*) malloc (sizeof (struct ListNode)); struct ListNode* temp = (struct ListNode*) malloc (sizeof (struct ListNode)); newlist = temp;while (L1 & & L2) {if (L1-> val)
< l2->Val) {temp- > next = L1; L1 = L1-> next;} else {temp- > next = L2; L2 = L2-> next;} temp = temp- > next;} temp- > next = L1? L1: L2: return newlist- > next;}
Language: cpp
/ * Definition for singly-linked list. * struct ListNode {* int val; * ListNode* next; * ListNode (int x): val (x), next (NULL) {} *}; * / class Solution {public: ListNode* mergeTwoLists (ListNode* L1, ListNode* L2) {ListNode newlist (INT_MIN); ListNode* temp = & newlist;if (L1 = = NULL & & L2 = NULL) {return NULL } if (L1! = NULL & & L2 = = NULL) {return L1;} if (L1 = = NULL & & L2! = NULL) {return L2;} while (L1 & & L2) {if (L1-> val)
< l2->Val) {temp- > next = L1; L1 = L1-> next;} else {temp- > next = L2; L2 = L2-> next;} temp = temp- > next;} temp- > next = L1? L1: L2; return newlist.next;}}
Language:python
# Definition for singly-linked list.# class ListNode (object): # def _ _ init__ (self, x): # self.val = x # self.next = Noneclass Solution (object): def mergeTwoLists (self, L1) L2): ": type L1: ListNode: type L2: ListNode: rtype: ListNode"result = cur = ListNode (0) while L1 and l2:if l1.val < l2.val: cur.next = L1L1 = l1.nextelse: cur.next = L2L2 = l2.next cur = cur .next cur.next = L1 or l2return result.next Thank you for your reading The above is the content of "how Linked List-Easy merges the two sorted linked lists". After the study of this article, I believe you have a deeper understanding of how Linked List-Easy merges the two sorted linked lists, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.