LeetCode problem brushing learning

Posted by nainil on Sun, 02 Jan 2022 06:21:54 +0100

First day of study

I use C + +, please forgive me for the mistakes. The original intention of the article is only to urge me to study. If it happens to be able to help you, I will be very happy.

preface

1, 141 Circular linked list

Title: give you a head node of the linked list to judge whether there are links in the linked list.

If there is a node in the linked list that can be reached again by continuously tracking the next pointer, there is a ring in the linked list. In order to represent the rings in a given linked list, the evaluation system uses the integer pos to represent the position where the tail of the linked list is connected to the linked list (the index starts from 0). If pos is - 1, there is no ring in the linked list. Note: pos is not passed as a parameter, but only to identify the actual situation of the linked list.

Returns true if there are links in the linked list. Otherwise, false is returned.

Source: LeetCode
Link: https://leetcode-cn.com/problems/linked-list-cycle
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Method 1: traverse the linked list. If the same occurs in the traversal process, it indicates that there is a ring
class Solution {
public:
    bool hasCycle(ListNode *head) {//
        unordered_set<ListNode*> seen;
        while (head != nullptr) {//The pointer field of the header node. If it is empty, it will be empty? But this is the pointer of the head node or the data field of the head node. If there is no result after traversal, false is returned
            if (seen.count(head)) {//If it has appeared before, it is not clear that only the data field should be judged here
                return true;
            }
            seen.insert(head);//If not, record it in the hash table
            head = head->next;//Point to next node
        }
        return false;


    }
};

1,unordered_set container

unordered_set container, which can be literally translated as "unordered set container", that is, unordered_ The set container is very similar to the set container. The only difference is that the set container will sort the stored data by itself, and unordered_ The set container does not.
Instead of storing data in the form of key value pairs, the value of the data is stored directly
I don't quite understand the usage of this here. My understanding is to create a hash table of linked list data type,

2. Floyd circle algorithm (also known as tortoise and rabbit race algorithm)

1. Define two fast and slow pointers. When the fast pointer "rings" the slow pointer, it means there is a ring. What I don't understand is why the fast pointer takes two steps at a time. What's the speed limit here?
2. Using the while loop, the slow pointer head starts and the fast pointer head Next starts; Using the do while loop, both the fast and slow pointers can start with head

class Solution {
public:
    bool hasCycle(ListNode *head) {//ListNode is defined by leetcode
   if (head == nullptr || head->next == nullptr) {
            return false;
        }
        ListNode* slow = head;
        ListNode* fast = head->next;
        while (slow != fast) {
            if (fast == nullptr || fast->next == nullptr) {
                return false;
            }
            slow = slow->next;
            fast = fast->next->next;
        }
        return true;



    }
};


class Solution {
public:
    bool hasCycle(ListNode *head) {//do-while
   if (head == nullptr || head->next == nullptr) {
            return false;
        }
        ListNode* slow = head;
        ListNode* fast = head;
        do
        {
            if (fast == nullptr || fast->next == nullptr) {
                return false;
            }
            slow = slow->next;
            fast = fast->next->next;
        }
        while (slow != fast); 
        return true;



    }
};

2, 21 Merge two ordered linked lists

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.
Input: l1 = [1,2,4], l2 = [1,3,4]
Output: [1,1,2,3,4,4]

1. Recursion

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


    }
};

3, 203 Remove linked list elements

Give you a head node of the linked list and an integer val. please delete all nodes in the linked list that meet the requirements Val = = Val's node and returns a new header node.

1. The definition of linked list is recursive, so linked list problems can often be solved by recursive method

This is very important, just like the string problem, which can often be solved by counting elements

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
         if (head == nullptr) {
            return head;
        }
        head->next = removeElements(head->next, val);
        return head->val == val ? head->next : head;


    }
};

summary

Topics: data structure leetcode linked list