数据结构 | 证明链表环结构是否存在

❤个人主页:

链表环结构

0.前言

在这篇博客中,我们将深入探讨链表环结构的检测方法:

Floyd算法的原理:如何通过快慢指针检测环?

环入口的定位:如何找到环的起点?

通过这篇博客,我会对链表中的环结构进行相关证明解释,总结学习。

1.环形链表(基础)

题目链接:https://leetcode.cn/problems/linked-list-cycle/description/

题目描述:

代码实现:

c 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
 
bool hasCycle(struct ListNode *head) {
    struct ListNode*slow,*fast;
    slow = fast = head;
    while(fast&&fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;

          if(slow==fast)
             return true;
    }
    return false;
}

代码解释:

这个题目的实现逻辑比较简单,我们定义快慢指针来进行实现,fast指针每次走2步,slow指针每次走1步,当快指针和慢指针相遇的时候,如果链表中存在环,则返回 true 。否则,返回 false。

2.环形链表Ⅱ(中等)

题目链接:https://leetcode.cn/problems/linked-list-cycle-ii/description/
题目描述:

代码实现1:

c 复制代码
struct ListNode* detectCycle(struct ListNode* head) {
    struct ListNode* fast;
    struct ListNode* slow;
    fast = slow = head;

    while (fast && fast->next)
    {
        //快慢指针依次走
        slow = slow->next;
        fast = fast->next->next;

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

代码解释1:

代码实现2:

c 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{
    struct ListNode* tailA=headA,*tailB=headB;
    int lenA=1,lenB=1;

    while(tailA)
    {
        tailA=tailA->next;
        ++lenA;
    }
    while(tailB)
    {
        tailB=tailB->next;
        ++lenB;
    }

    int gap=abs(lenA-lenB);
    struct ListNode* longlist=headA,*shortList=headB;

    if(lenA<lenB)
    {
        longlist=headB;
        shortList=headA;
    }

    while(gap--)
    {
        longlist=longlist->next;
    }

    while(longlist!=shortList)
    {
        longlist=longlist->next;
        shortList=shortList->next;
    }
    return longlist;

}

struct ListNode *detectCycle(struct ListNode *head) {
    struct ListNode* fast;
    struct ListNode* slow;
    fast=slow=head;

    while(fast&&fast->next)
    {
        //快慢指针依次走
        slow=slow->next;
        fast=fast->next->next;

        if(slow==fast)
        {
            //转换成求交点
            struct ListNode* meet=slow;
            struct ListNode* lt1=meet->next;
            struct ListNode* lt2=head;
            meet->next=NULL;
            return getIntersectionNode(lt1,lt2);
        }
    }
    return NULL;
}

代码解释2:

3.证明相遇条件及结论

3.1 问题1特殊情况证明

问题1: 为什么slow走1步,fast走2步,他们会相遇吗?会不会错过?请证明

3.2 问题1普适性证明

问题:为什么slow走1步,fast走3步(x>=3),他们会相遇吗?会不会错过?请证明

感谢您的阅读支持!!!
后续会持续更新的!!!
文末投票支持一下!!!

相关推荐
白白白小纯18 小时前
算法篇—返回倒数第k个节点
c语言·数据结构·算法·leetcode
无忧.芙桃18 小时前
数据结构之堆
c语言·数据结构·c++·算法·
小蒋学算法19 小时前
算法-删除元素后最大固定点数目-典型最长增长序列算法
数据结构·算法
白白白小纯20 小时前
算法篇—链表的中间节点
c语言·数据结构·算法·leetcode
pluviophile_s21 小时前
数据结构:第6讲:树与二叉树
数据结构·笔记
来一碗刘肉面1 天前
字符串模式匹配(朴素模式匹配算法与KMP算法)
数据结构·算法
code bean2 天前
【C#】 `Channel<T>` 深度解析:生产者-消费者模式的现代解法
数据结构·c#
storyseek2 天前
前缀和实现Kogge-Stone算法
数据结构·算法
元Y亨H2 天前
深度解构:数据结构与算法的理论基石与工程演进
数据结构·算法
元Y亨H2 天前
数据结构与算法的通俗指南
数据结构·算法