代码随想录 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的链表题

相关推荐
Pniubi9 小时前
01背包类问题-力扣416.分割等和子集
算法·leetcode·职场和发展
eggwyw19 小时前
C++ union 的一种妙用法
开发语言·c++
罗西的思考20 小时前
【轻量化复现大模型 / KIMI K3】MTP / EAGLE‑3 模块交互式演示
人工智能·深度学习·算法
ShineWinsu21 小时前
对于Qt:QHBoxLayout常见属性以及方法的解析
c++·qt·面试·gui·客户端·布局管理器·水平布局器
小七在进步1 天前
string的实现(1)
c++·笔记·visual studio
kyle~1 天前
模型预测控制(MPC)
算法·机器人·控制·传感器
敲上瘾1 天前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
牢姐与蒯1 天前
cpp算法之哈希
c++·算法·哈希算法
开开心心就好1 天前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
lcj25111 天前
[C++] C++11
开发语言·c++·笔记