[26] Merge two ordered linked lists | Delete duplicate elements in the sorted linked list (LeetCode 21 | 83)

Combine two ordered linked lists

Problem Description

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

Problem-solving ideas

The first thing that comes to mind is to scan the nodes of L1 one by one. For each node, find the position where the former is less than or equal to it and the latter is greater than it in L2, and insert the node of L1 into it. To this end, you need to add a head node to L2, the value of the head node is assigned to -101, that is, the minimum value of the node given by the title -1. But I couldn't achieve it with code...

The following is from the official solution:

Iterative method:

Set a sentinel node prehead for the final return result. Maintain a prev pointer, and point its next to the node with the smaller value of the current l1 and l2 each time, and repeat until l1 or l2 points to null. At the end, the linked list that has not been traversed is connected to the end.

class Solution {
    
    
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
    
    
        ListNode* preHead = new ListNode(-1);

        ListNode* prev = preHead;
        while (l1 != nullptr && l2 != nullptr) {
    
    
            if (l1->val < l2->val) {
    
    
                prev->next = l1;
                l1 = l1->next;
            } else {
    
    
                prev->next = l2;
                l2 = l2->next;
            }
            prev = prev->next;
        }
        // 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
        prev->next = l1 == nullptr ? l2 : l1;

        return preHead->next;
    }
};

Recursion:

Here is an interesting thing to share first, the recursive debugging method shared by the big guys seen in the public account, although it is not used in this question, but it does not affect my thinking about it- recursive debugging tips .

The recursive thought of this question:

If l1 or l2 is empty, there is no need to merge, and a non-empty linked list is returned directly. Otherwise, if the current value of l1 is less than l2, then point l1's next to the linked list of l1's next and l2 (that is, put the big value after the small value); otherwise, point l2's next to l2's next Linked list after merging with l1.

class Solution {
    
    
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
    
    
        if (l1 == nullptr) {
    
    
            return l2;
        } else if (l2 == nullptr) {
    
    
            return l1;
        } else if (l1->val < l2->val) {
    
    
            l1->next = mergeTwoLists(l1->next, l2);
            return l1;
        } else {
    
    
            l2->next = mergeTwoLists(l1, l2->next);
            return l2;
        }
    }
};

Delete duplicate elements in the sorted list

Problem Description

Given a sorted linked list, delete all duplicate elements so that each element appears only once.

Problem-solving ideas

The idea is to use the cur pointer to point to the current node, use a temp to store the value of the current node, and then compare it with the next node value, if it is equal, delete the next node, if it is not equal, the cur pointer advances by one.

class Solution {
    
    
public:
    ListNode* deleteDuplicates(ListNode* head) {
    
    
        if(!head)   return head; //链表为空
        ListNode* cur = head;
        int temp;//存储当前结点的值
        while(cur->next){
    
    
            temp = cur->val;
            if(cur->next->val == temp){
    
    //若下一个结点的值与当前结点值相等则删除下一结点
                ListNode* p = cur->next;
                cur->next = p->next;
                delete p;
            }else{
    
    //否则cur前进一个结点
                cur = cur->next;
            }
        }
        return head;
    }
};

Time complexity: O(n) traverse all nodes once.
Space complexity: O(1)

Experience

555... The linked list is so difficult... I feel that I haven't found the question of the linked list, especially when I was merging two ordered linked lists, and I went through the process manually. The final compilation result was still wrong. It's a test of patience. Then, the recursive solution to the linked list question is really hard to come up with! The main purpose is to use recursion when doing other types of questions. You can put aside the details of the data structure and directly understand the meaning of recursion, but in the face of a linked list, I can’t figure out how it is recursive, downward recursion and upward return when the linked list How is it broken and merged?

Guess you like

Origin blog.csdn.net/qq_43424037/article/details/113405911