2021-04-24 LeetCode 21. Merge two ordered linked lists

Summary of linked list related knowledge:

1. NULL pointer: nullptr is recommended instead of NULL
2. Pay attention to the definition of the linked list given in the title and be flexible
3. Create an empty node

ListNode list=new ListNode() initializes an empty node. The default node value is 0 (see the definition in the topic for details)
ListNode list=new ListNode(0) initializes an empty node with a node value of 0

4. Distinguish between int* a and int* a
http://www.cppblog.com/tiger7/articles/146072.html
5. Use - > to obtain the val value p - > val; Point to next Q - > next
6.if () should compare the size of values, that is, the size of val values in l1 and l2, rather than comparing the l1 and l2 linked lists themselves

7.!!! (at the pit, it's my fault that I'm too delicious ╮ (╯▽ ╭)
The next values of l3 and r defined are nullptr, so l1 should point to r - > next instead of r when merging nodes. Otherwise, no nodes will be merged into the l3 linked list from beginning to end, and the final code return result will be null

  • Error writing process analysis:
    Enter the ① cycle of while:
    r = l1;// L1 points to R itself
    r = r->next; // Observe the ListNode definition given by the topic. The next values of l3 and R defined are nullptr, so r is equivalent to being assigned nullptr at this time

  • Enter the ② while cycle:
    r = l1; // Since R is assigned nullptr at the end of the first cycle, L1 points to nullpte and is not merged into R (i.e. l3 linked list)

  • Therefore, all subsequent loops are invalid o(╥﹏╥) O

8. Finally, l3 - > next is returned, because l3 itself is a header pointer, and the val value is 0 by default. From l3 - > next, it is the first node in the consolidated linked list, and the corresponding val value is the first value (i.e. the minimum value) in the consolidated linked list

  • Error code analysis:
    return l3;// A header pointer is added at the front of the final result. The default value is 0
    Error results under test cases [0,1,1,2,3,4,4], actual correct results [1,1,2,3,4,4]

The code is as follows:

Iterative method

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {} //There is no parameter value. The default val is 0 and next is nullptr
 *     ListNode(int x) : val(x), next(nullptr) {} //There is one parameter x, default val is x and next is nullptr
 *     ListNode(int x, ListNode *next) : val(x), next(next) {} //There are two parameters X and * next. The default val is x and next is next
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        /**
         * Define pointer
         */
        ListNode* l3 = new ListNode(0);//Define a pointer to a null node as a sentinel node (according to the definition of ListNode provided by the title, it can be ListNode() or ListNode(0) or ListNode(-1))
        ListNode* r = l3;//Define another pointer r, pointing to the same place as l3

        /**
         * Find smaller node merge
         */
        //If the L1 and L2 linked lists are not empty, execute the while loop
        while(l1 && l2){
            //If the val of l1 is less than that of l2, the next of the sentinel pointer points to l1 (i.e. the smaller one)
            if(l1->val <= l2->val){
               r->next = l1;
               l1 = l1->next;//l1 pointer moves back one
            }else{
               r->next = l2;//Conversely, similarly, it points to l2 (i.e. the small one)
               l2 = l2->next;//l2 move the pointer back one
            }
           r = r->next;//In if else, R - > next has been updated. Here, move r backward one. If the conditions are met, continue the cycle
        }

        /**
         * Merge remaining nodes
         */        
        r->next = l1 != nullptr ? l1:l2;//Question mark expression: judge which of L1 and L2 is not empty, then R - > next points to which, and directly splice all remaining nodes into l3
        return l3->next;
    }
};

Title:

Merge the two ascending linked lists into a new ascending linked list and return. The new linked list is composed of all nodes of a 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]

Tips:
The node number range of the two linked lists is [0, 50]
-100 <= Node.val <= 100
l1 and l2 are arranged in non decreasing order

Added by romic on Tue, 18 Jan 2022 14:06:16 +0200