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

相关推荐
txinyu的博客25 分钟前
解析muduo源码之 SocketsOps.h & SocketsOps.cc
c++
ctyshr1 小时前
C++编译期数学计算
开发语言·c++·算法
zh_xuan1 小时前
最小跳跃次数
数据结构·算法
努力写代码的熊大1 小时前
c++异常和智能指针
java·开发语言·c++
yumgpkpm1 小时前
2026软件:白嫖,开源,外包,招标,晚进场(2025年下半年),数科,AI...中国的企业软件产业出路
大数据·人工智能·hadoop·算法·kafka·开源·cloudera
John_ToDebug1 小时前
WebContent 与 WebView:深入解析浏览器渲染架构的双层设计
c++·chrome·ui
千秋乐。1 小时前
C++-string
开发语言·c++
孞㐑¥1 小时前
算法—队列+宽搜(bfs)+堆
开发语言·c++·经验分享·笔记·算法
yufuu981 小时前
并行算法在STL中的应用
开发语言·c++·算法
zh_xuan1 小时前
单青蛙跳台阶
数据结构·算法