Leetcode---LeetCode141/142. Ring Linked List (I) and (II) (detailed code explanation + flowchart + mathematical logic expansion)


foreword

"There are scenery in front of the mountain and behind the mountain, and you are free with the wind and without the wind."
The content of this chapter is the code analysis and flow chart of some methods for the daily random question of Likou


提示:以下是本篇文章正文内容,下面案例可供参考

141. Circular Linked List I

Give you the head node head of a linked list, and judge 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.
insert image description here

1.1 Links:

141. Ring linked list I link

1.2 Ideas:

Define a fast pointer (two steps at a time) and a slow pointer (one step at a time) to convert it into a race between the tortoise and the hare,In the end, they will all enter the ring. When the fast pointer catches up with the slow pointer, it proves that there is a ring., if there is no ring, there will be no situation where the fast pointer catches up with the slow pointer. When the fast pointer is NULL, it will be over.

1.3 Code: fast and slow pointer

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

1.4 Flowchart:

insert image description here

142. Circular Linked List II

Given the head node head of a linked list, return the first node of the linked list starting to enter the ring. Returns null if the linked list is acyclic.
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). If pos is -1, there are no cycles in the list. Note: pos is not passed as a parameter, it is just to identify the actual situation of the linked list.
The linked list is not allowed to be modified.
insert image description here

2.1 Links:

142. Circular Linked List II link

2.2 Ideas:

A pointer walks from the meeting point, and a pointer walks from the head of the linked list, and they will meet at the entry point. Special inference: general inference:

insert image description here

insert image description here

2.3 Code:

struct ListNode *detectCycle(struct ListNode *head) 
{
    
    
    struct ListNode *fast=head;
    struct ListNode *slow=head;
    struct ListNode *meet=NULL;
    while(fast&&fast->next)
    {
    
    
        fast=fast->next->next;
        slow=slow->next;
        if(slow==fast)
        {
    
    
            meet=slow;
            while(meet!=head)
            {
    
    
                meet=meet->next;
                head=head->next;
            }
            return head;
        }
    }
    return NULL;
}

2.4 Flowchart:

insert image description here

Extended questions and proofs (interview frequently asked):

3.1 Will slow and fast meet? (one step for slow, two steps for fast)

3.1.1 Answer: (1 reduction each time) will definitely meet

3.1.2 Proof:

insert image description here

3.2 Is it okay to take 1 step in slow and n(3/4/5.…) steps in fast? (n > 2)

3.2.1 Answer: not necessarily meet

3.2.2 Proof:

insert image description here


Summarize

Ending, this is the end of today's Likou daily question content. If you want to know more in the future, please follow me. There are many methods that have not been written. I hope you guys can add them~

Guess you like

Origin blog.csdn.net/ljq_up/article/details/130533872