Interview hot topic: circular linked list and circular linked list to find the ring entry node problem

circular linked list

Question:
Given you a head node head of a linked list, determine whether there is a ring in the linked list.
If there is a node in the linked list that can be reached again by continuously tracking the next pointer, then there is a cycle in the linked list. In order to represent the ring in the given linked list, the evaluation system internally uses the integer pos to indicate the position where the end of the linked list is connected to the linked list (the index starts from 0). Note: pos is not passed as a parameter. Just to identify the actual situation of the linked list.
Returns true if there is a cycle in the linked list. Otherwise, returns false.
Source: LeetCode circular linked
insert image description here
list Idea 1: Violent solution
We traverse the linked list from the beginning, and every time we traverse a node, we check whether the node has already appeared from the beginning. This is the first brute force solution we can think of! Time complexity O(N^2) space complexity O(1).

Idea 2: Fast and slow pointers
We create two pointers, slow and fast, and let them point to the head node at the same time. Slow takes one step at a time, and fast takes two steps at a time. If the final result of the loop is slow=fast then the linked list is a ring, if fast=nullptr then the linked list is not a ring.

insert image description here

insert image description here
insert image description here
The reason is the same as two people running together. The track is circular and keeps running. The fast person will definitely catch up with the slow person after starting to run on the same starting line.
code:

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

Error-prone points:
insert image description here

The entry node of the ring in the linked list

Question:
Given a linked list, return the first node from which the linked list starts entering the ring. Starting from the head node of the linked list and following the next pointer into the ring, the first node is the entry node of the ring. Returns null if the linked list is acyclic.
To represent a cycle in a given list, we use the integer pos to denote the position in the list where the tail of the list joins (indexes start at 0). If pos is -1, there are no cycles in the list. Note that pos is only used to identify the ring and will not be passed as a parameter to the function.
Description: Modification of the given linked list is not allowed. Source: The entry node of the ring in the
LeetCode linked list Idea: First prove that the linked list has a ring, and then find the entry node. If there is a ring, then we must be walking slowly and half the distance traveled by fast, and look at the analysis in the figure below
insert image description here


insert image description here

insert image description here
insert image description here
code:

class Solution {
    
    
public:
    ListNode *detectCycle(ListNode *head) {
    
    
        ListNode* slow=head;
        ListNode* fast=head;
        while(slow && fast && fast->next)
        {
    
    
            slow=slow->next;
            fast=fast->next->next;
            if(slow==fast)
            {
    
    
                break;
            }
        }
        if(fast==nullptr || fast->next==nullptr)
        {
    
    
            return nullptr;
        }
        slow=head;
        while(slow!=fast)
        {
    
    
            slow=slow->next;
            fast=fast->next;
        }
        return fast;
    }
};

Guess you like

Origin blog.csdn.net/DEXTERFUTIAN/article/details/129338014