[Leedcode] Necessary interview questions and proof questions for circular linked lists (with pictures)

Necessary interview questions and proof questions for circular linked lists (with diagrams)



foreword

This article introduces two interview questions related to the circular linked list and the extended proof questions (with source code + diagram)


1. The first question

1. Topic

141. Ring linked list: as follows (example):

给你一个链表的头节点 head ,判断链表中是否有环。

如果链表中有某个节点,可以通过连续跟踪next指针再次到达,则链表中存在环。
为了表示给定链表中的环,评测系统内部使用整数pos来表示链表尾连接到链表中的位置(索引从0开始).
注意:pos 不作为参数进行传递。仅仅是为了标识链表的实际情况。

如果链表中存在环 ,则返回 true  否则,返回 false 

insert image description here


insert image description here


2. Ideas

slow and fast point to the beginning of the linked list, slow takes one step at a time, fast takes two steps at a time, fast without a ring will go to the empty, fast with a ring will catch up with slow, as shown in the figure below !
insert image description here


3. Code

The code is as follows (example):

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

4. Extended Questions

(1) Proof Question 1:

Topic: As follows (example):
Why do slow and fast meet in the ring? Will you miss it in the ring and never meet it?


Conclusion: As follows (example):
They will definitely meet, but proof is needed!


Proof of analysis: as follows (example):

insert image description here


insert image description here


(2) Proof Question 2:

Topic: As follows (example):
Why do slow take one step and fast take two steps? Can fast take n steps at a time (n>2)?


Conclusion: As follows (example):
When n>2, they may not meet each other and need to be proved!


Proof of analysis: as follows (example):

insert image description here


insert image description here


insert image description here


2. The second question

1. Topic

  1. Ring linked list 2: as follows (example):
给定一个链表的头节点head,返回链表开始入环的第一个节点。如果链表无环,则返回null。

如果链表中有某个节点,可以通过连续跟踪next指针再次到达,则链表中存在环。
为了表示给定链表中的环,评测系统内部使用整数pos来表示链表尾连接到链表中的位置(索引从0开始)。
如果pos是-1,则在该链表中没有环。注意:pos不作为参数进行传递,仅仅是为了标识链表的实际情况。

不允许修改链表。

insert image description here


2. Ideas + extended proof questions

Idea/Conclusion: As follows (example):

A pointer starts from the meeting point, and a pointer starts from the head of the linked list, and they will meet at the entry point of the ring!


The proof method (1) is shown in the figure below

insert image description here


insert image description here


insert image description here


The proof method (2) is shown in the figure below

insert image description here


Summarize

The above is what I want to talk about today. This article introduces the necessary interview questions and proof questions for circular linked lists (solutions with attached pictures).
If my blog is helpful to you, remember to support it three times. Thank you for your support!
insert image description here

Guess you like

Origin blog.csdn.net/2201_75587702/article/details/129211309