【快慢指针】个人练习-Leetcode-142. Linked List Cycle II

SEED-VIG数据集实战:5步搞定驾驶员疲劳检测模型(附Python代码) 本文提供了一份基于SEED-VIG数据集的驾驶员疲劳检测模型实战指南。通过五个核心步骤——数据准备、特征提取、模型训练、优化评估及部署思路,详细讲解了如何利用脑电信号构建分类器,并附有完整的Python代码,帮助开发者快速上手这一智能座舱关键技术。 阅读详情

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

题目大意:给一个链表的头部,判断链表是否有环,如果有,返回环的第一个指针;如果没有,返回nullptr

思路:简单的思路是并查集,第二次插入的那个指针就是环的起点。但这样空间复杂度还是O(N)O(N)O(N)。使用快慢指针可以让空间复杂度降为O(1)O(1)O(1)

快慢指针都从头部开始,slow每次走1步,fast每次走2步,如果链表有环,那么必然fast会追上slow。如果无环,某一次fast的下一步就是null

判断很好完成,那么如何找到入环的点呢?假设环外长度为aaa,环起点到相遇点长度为bbb,环剩下长度为ccc,那么总环长为b+cb+cb+c,总链表长为a+b+ca+b+ca+b+c

因为fast走过的路程为slow的两倍,假设fast走了nnn圈,那么有
a+n(b+c)=2(a+b) a+n(b+c)=2(a+b) a+n(b+c)=2(a+b)
得出
a=(n−1)(b+c)+c a=(n-1)(b+c)+c a=(n1)(b+c)+c
那么此时再让一个指针ptr从头开始,其走到环的起点的步数为aaa,刚好就是slow指针把【当前环剩下的步数走完】并且【再走n−1n-1n1圈环】的步数。因此它们一起走,一定会在环的起点相遇,这就找到环的起点了。

完整代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *fast = head, *slow = head;
        while (fast != nullptr) {
            slow = slow->next;
            if (fast->next == nullptr)
                return nullptr;
            fast = fast->next->next;
            if (fast == slow) {
                ListNode *ptr = head;
                while (ptr != slow) {
                    ptr = ptr->next;
                    slow = slow->next;
                }
                return ptr;
            }
            
        }
        return nullptr;
    }
};
2024-12月js逆向案例-sensor-data之vmp字段之akamai_2/3.0-(下) akamai2/3的vmp字段逆向分析 阅读详情

相关推荐

【笔记篇】Davinci Configurator EcuM模块

autosar ecum模块简介

weixin_47571898的博客 1230

leetcode-Python】-快慢指针-142. Linked List Cycle II

题目链接 https://leetcode.com/problems/linked-list-cycle-ii/ 题目描述 给定一个链表,如果链表中有环,返回链表中环的起始位置。如果没有环则返回null。 示例 给定链表: 输出:1(索引为1的节点为环的起点) 解题思路 首先我们通过【leetcode-Python】-快慢指针-141. Linked List Cycle判断链表中是否有环,fast指针一次走两步,slow指针一次走一步,如果发现fast指针和slow指针相遇则跳出.

gulaixiangjuejue的博客 347

ArcGIS实验教程——实验十七:缓冲区分析(Buffer Analysis)

【实验描述】缓冲区(Buffer)是为了识别某一地理实体对周围地物的影响而在其周围建立的一定宽度多边形区域,缓冲区分析(Buffer Analysis)是用来确定不同地理要素的空间临近性或接近程度的一种分析方法。作为GIS的空间分析方法之一,缓冲区分析的应用范围特别广泛,常用于分析矢量实体对周围的影响。例如,工厂排放的废水废气所影响的空间范围等等。 一、缓冲区的基本概念 1、缓冲区 是地理空间目标的一种影响范围或服务范围。 2、缓冲区分析 指根据分析对象的点、线、面实体,自动建立它们周围一定距离的带

智慧空间实验室 1万+

(Leetcode 142)Linked List Cycle (II) (快慢指针详解)

Leetcode141题和142题很相似,都涉及到了快慢指针算法快慢指针就是设置两个指针,一个快指针,一个慢指针来达到解题的目的。下面分为以下几个模块来讲解: 解第141题解第142题为什么快指针一定要设置为慢指针的2倍快慢指针的拓展应用 一、Leetcode 141题 题目为:Linked List Cycle Given a linked list, determi

willduan的博客 1万+

Leetcode142. Linked List Cycle II

判断链表是否有环,并把环的入口点找到

magic_jiayu的博客 339

Leetcode 142 Linked List Cycle II

Leetcode 142 Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos w...

博学 笃志 切问 近思 260

LeetCode 142. 环形链表 IILinked List Cycle II

易错点说明忘记判空 fast 或 fast.next可能导致访问空指针错误相遇后直接返回 slow要重新设置 head 与 slow 同时移动比较节点时用值而非引用比较应比较节点对象本身,而非 val 值快慢指针(Floyd判圈算法)不仅能判断有无环,还能找出环入口。核心推导公式,理解本质,掌握做题模板。

2501_91641025的博客 926

LeetCode 142Linked List Cycle II(C++ Java Python)

题目:http://oj.leetcode.com/problems/linked-list-cycle-ii/ Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using ex

龙之梦 3063

LeetCode(142)Linked List Cycle2

题目如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 题目分析: 本题是上一道题目的扩展。可以沿用上一题目的方式。使用一块一慢

BUILD 2286

(Leetcode 142)Linked List Cycle (II) (快慢指针详解)

Leetcode141题和142题很相似,都涉及到了快慢指针算法快慢指针就是设置两个指针,一个快指针,一个慢指针来达到解题的目的。下面分为以下几个模块来讲解: 解第141题 解第142题 为什么快指针一定要设置为慢指针的2倍 快慢指针的拓展应用 一、Leetcode 141题 题目为:Linked List Cycle Given a linked list, determine i...

zhunode的探索 327

LeetCode //C - 142. Linked List Cycle II

【代码】LeetCode //C - 142. Linked List Cycle II

Made in Code 1017

NYUer | LeetCode142 Linked List Cycle II

NYUer | LeetCode142 Linked List Cycle II

Moses_SU的博客 237

LeetCode 142. Linked List Cycle II(环形链表II) -- c语言

142. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which rep...

d_benhua的博客 423

LeetCode 142 环形链表 II Linked List Cycle II Python

链表定义 # Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None 142. 环形链表 II Linked List Cycle II LeetCodeCN 第142题链接 第一种方法还是上面的用哈希表s...

fongim的博客 397

[LeetCode][142] Linked List Cycle II

[LeetCode][142] Linked List Cycle II lower 和faster 如果有重合就说明有环 当有环时候 头节点到入口点为L1 入口点到会合点是L2 循环长度为c 第一次快慢指针相遇是进行了n轮的循环 lower长度是L1+L2 faster长度是L1+L2+nc 由此第二次遇到后可以求出c 因为faster每次走两步 lower每次走一步 因此 (L1+L2 )2=...

OCEANtroye的博客 251

LeetCode 142. Linked List Cycle II快慢指针

Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represen...

da_kao_la的博客 273

快慢指针LeetCode 142 Linked List Cycle II

142. Linked List Cycle II 题目描述: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next

qq_41243742的博客 229

LeetCode 142. Linked List Cycle II 题解

本题是链表的经典问题,主要考察对链表操作和双指针技巧的理解和应用。通过使用快慢指针,我们可以不仅检测链表中是否存在环,还可以找到环的入口。双指针的核心思想是:通过让快指针以两倍的速度移动,当链表中存在环时,快指针最终会追上慢指针。然后将其中一个指针重置到链表头部,以相同的速度移动两个指针,它们再次相遇的点就是环的入口。这种方法不仅适用于环形链表 II 问题,还可以应用于许多其他链表相关的问题,例如相交链表、链表中点查找等。掌握双指针技巧,对于解决链表问题非常重要。

cannonjinx的博客 309

Leetcode 142 Linked List Cycle

**Leetcode 142 Linked List Cycle Ⅱ 题目描述 Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer...

qq_43271202的博客 209

LeetCode 142. Linked List Cycle II

LeetCode 142. Linked List Cycle II 142.Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. To represe...

豆子 153

力扣(合并区间)

给定一组可能重叠或相邻的整数区间,合并所有重叠或端点相接的区间,返回不重叠的最小区间列表。例如输入 [[1,3],[2,6],[8,10],[15,18]],输出 [[1,6],[8,10],[15,18]]。核心是先按左端点升序排序,再单次线性扫描完成合并,避免暴力两两比较。

weixin_52811458的博客 300

天心ERP数据表

齐全的天心ERP数据表,如果在使用天心ERP的朋友们得到此数据表,则会如虎添翼

华为ICT大赛2022-2023网络赛道国赛实验ESNP(附完整详细答案)

华为ICT大赛2022-2023网络赛道国赛实验ESNP(附完整详细答案)

上一篇: 【层序遍历】个人练习-Leetcode-102. Binary Tree Level Order Traversal
下一篇: 【二分】个人练习-Leetcode-793. Preimage Size of Factorial Zeroes Function
Rstln
博客等级 码龄8年 118粉丝 · 190原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值