代码随想录 Leetcode142. 环形链表 II

题目:


代码(首刷看解析 2024年1月13日):

cpp 复制代码
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        if (head == nullptr) return nullptr;
        ListNode* fast = head;
        ListNode* slow = head;
        while (true) {
            if(fast->next == nullptr || fast->next->next == nullptr) return nullptr;
            fast = fast->next->next;
            slow = slow->next;
            if(fast == slow) break;
        }
        fast = head;
        while (fast != slow) {
            fast = fast->next;
            slow = slow->next;
        }
        return fast;
    }
};

双指针解决百分之99的链表题

相关推荐
Non-existent98734 分钟前
海拔批量查询 + 批量 KML 生成工具-WPS 插件 TableGIS 新功能
javascript·c++·excel·wps
咩咦7 小时前
C++学习笔记28:静态成员应用:不用循环求1到n的和
c++·学习笔记·类和对象·static·构造函数·oj·静态成员
EllinY8 小时前
CF2217E Definitely Larger 题解
c++·笔记·算法·构造
筠筠喵呜喵8 小时前
Linux软件开发性能优化
linux·c++·性能优化
Bruce_kaizy9 小时前
c++ linux环境编程——文件io介绍以及open 、write 、read 三剑客深度详解
linux·服务器·c++·ubuntu·操作系统·文件io
PAK向日葵11 小时前
我用 C++ 写了一个轻量级 Python 虚拟机,刚刚开源
c++·python·开源
玖釉-11 小时前
下一个排列:从字典序到原地算法的完整推导
数据结构·c++·windows·算法
IronMurphy11 小时前
【算法五十】62. 不同路径
算法
影寂ldy11 小时前
C#一维数组
算法