【C语言/数据结构】经典链表OJ习题~第二期——链中寻环

🎈🎈🎈欢迎采访小残风的博客主页:残风也想永存-CSDN博客🎈🎈🎈

🎈🎈🎈本人码云  链接:残风也想永存 (FSRMWK) - Gitee.com🎈🎈🎈

 有什么疑问,皆可打在评论区下,24小时不定时间进行答疑哦~,下面进入本期的主题——环形链表的求解~

一、环形链表Ⅰ

1.题目展示

2.题目链接

141. 环形链表 - 力扣(LeetCode)

3.思路讲解

4.代码实现
/**
 * Definition for singly-linked list.
 * 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(fast == slow)
            return true;
    }
    return false;
}
5.扩展问题

a.证明:慢指针走一步,快指针走两步一定可以?

b.证明:快指针走三步、四步、....可行吗?

二、环形链表Ⅱ

1.题目展示

2.题目链接

142. 环形链表 II - 力扣(LeetCode)

3.思路讲解

4.代码实现
 /**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head)
 {
    struct ListNode *slow = head,*fast = head;
    while(fast && fast ->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if(fast == slow)
        {
            struct ListNode * cur = head;
            while(cur)
            {
                if(cur == slow)
                    return cur;
                cur  = cur->next;
                slow = slow->next;
            }
        }
    }
    return false;
}

相关推荐

  1. 数据结构-习题C++)

    2024-05-02 00:12:04       66 阅读
  2. 数据结构英文习题解析-第二List

    2024-05-02 00:12:04       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-02 00:12:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-02 00:12:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-02 00:12:04       87 阅读
  4. Python语言-面向对象

    2024-05-02 00:12:04       96 阅读

热门阅读

  1. 负二进制转换

    2024-05-02 00:12:04       37 阅读
  2. 椋鸟C++笔记#1:C++初识

    2024-05-02 00:12:04       27 阅读
  3. Pytoch实现姿势识别模型

    2024-05-02 00:12:04       27 阅读
  4. leetcode394. 字符串解码

    2024-05-02 00:12:04       31 阅读
  5. 【MATLAB】MATLAB App Designer中的回调函数

    2024-05-02 00:12:04       35 阅读
  6. 李沐69_BERT训练集——自学笔记

    2024-05-02 00:12:04       33 阅读