Leetcode 21. Two methods of merging two ordered linked lists C language 100%

【topic】

Combine the two ascending linked lists into a new ascending linked list and return. The new linked list is composed of all the nodes of the given two linked lists. 

Examples:

Input: 1-> 2-> 4, 1-> 3-> 4
Output: 1-> 1-> 2-> 3-> 4-> 4

【Thinking】

There are two ideas, one of which is implemented through recursion.

The recursive equation is

1 {
2      l1[0] + merge(l1->next, l2)      l1 < l2
3      l2[0] + merge(l1, l2->next)      l2 < l1  
4 }

First compare the size of the first number of the two linked lists, select the smaller one, and then connect the recursive sequence.

【Code】

 

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 
 9 
10 struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
11     if (!l1)
12         return l2;
13     else if (!l2)
14         return l1;
15     else {
16         if (l1->val < l2->val) {
17             l1->next = mergeTwoLists(l1->next, l2);
18             return l1;
19         }
20         else {
21             l2->next = mergeTwoLists(l1, l2->next);
22             return l2;
23         }
24     }
25 }

First define the recursion condition, when l1 or l2 is empty, return another non-empty linked list.

Then for both non-empty, compare the size of l1 l2, choose a smaller head to connect the rearranged sequence, and return to the head

【result】

0ms 100%

5.6MB 100%

 

【Thinking】

Iterative processing

First build a chain head, and finally use it as a pointer when returning.

Moreover, in order to simplify the operation, instead of pulling out each chain and connecting it to the chain head, you can directly point the chain head to l1 or l2, so that if you want to connect several chains on the same chain at once, or another list is empty At this time, no extra operations are required.

In addition, as the end of the final ordered table, a pointer must be set to connect the two tables.

【Code】

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 
 9 
10 struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
11     if (!l1)
12         return l2;
13     else if (!l2)
14         return l1;
15     struct ListNode *pre, *n;
16     pre=(struct ListNode*)malloc(sizeof(struct ListNode));//
17     pre->val = -1;
18     n = pre;
19     while (l1 && l2) {//
20         if (l1->val >= l2->val) {
21             n->next = l2; 
22             l2 = l2->next;
23             n = n->next;
24         }
25         else {
26             n->next = l1;
27             l1 = l1->next;
28             n = n->next;
29         }
30     }
31     if (l1)//
32         n->next = l1;
33     else
34         n->next = l2;
35     return pre->next;
36     
37 }

[BUG analysis]

I actually forgot line31-34

The condition for looping out is that neither l1 nor l2 is empty, when the last jump out! There must be a table whose last one is relatively small, and then reaches the end, the last n must not be connected to another table! So be sure to deal with the tail in the end!

Guess you like

Origin www.cnblogs.com/jhjgarden/p/12711707.html