当前位置:网站首页>Worm ring list
Worm ring list
2022-04-21 14:21:00 【Hua Weiyun】
Ring chain
Circular list
subject

analysis


== Let's dissect the code ==

bool hasCycle(struct ListNode *head) { struct ListNode* fast = head,*slow = head; while(fast && fast->next) { fast = fast->next->next; slow = slow->next; if(fast == slow) return true; } return false;}
Extension problem :
==1. Why? fast and slow Will meet in the ring , Will there be such a situation . Is always staggered in the ring, never meet ? Please prove .==
Conclusion : Just up there fast and slow It must have met
prove :
First step :slow and fast,fast It must be an advanced ring , At this time slow yes fast Half the distance before entering the ring .
The second step : With slow Ring entry ,fast I've been walking in the ring for a while , go How much depends on the size of the ring
The third step : Let's assume that slow When entering the loop, the distance is the same as fast yes ==N==
slow Every time you go forward 1 Step ,fast Two steps forward , Every chase , Judge the meeting , The result is ==N-1==, In other words, the distance between them is decreasing step by step every time they chase , Until they met

== Another problem arises here is slow And fast As long as the step difference is one, you can meet ==
==2. Why? slow Take a step ,fast Two steps ?fast Can you take more than two steps ?==
Conclusion :fast go n Step ,n>2, Not necessarily meet
Here is an analysis of slow Take a step ,fast The staggered and non staggered situation of taking three steps
The distance between them changes in pairs at a time , This means that it may be staggered

== The above parity problem arises again N It's their multiple of step difference that they can meet ==
Circular list II
subject

== How to find the entry point of a ring ==
analysis
Let's go straight to the conclusion == A pointer starts from the meeting point , A pointer starts from the chain header , They will meet at the entrance of the ring ==


struct ListNode *detectCycle(struct ListNode *head) { struct ListNode * fast = head,* slow = head; while(fast && fast->next) { fast = fast->next->next; slow = slow->next; if(slow == fast)// meet { // Meet node struct ListNode * meetNode = fast; while(meetNode != head) { meetNode = meetNode->next; head = head->next; } return meetNode; } } return NULL;}
版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211406369780.html
边栏推荐
猜你喜欢

如何以Sonar为例创建一个适用与所有企业的测试步骤

Tcpdump packet capture and nmap are easy to use

一篇文章带你玩转c语言变量和数据类型

Ros2 learning notes (8) -- road recognition and debugging based on the application of ros2 parameters
![[groovy] mop meta object protocol and meta programming (use groovy meta programming for function interception | dynamic interception function | dynamically obtain methods in metaclass | evaluate metho](/img/3f/a4378551e5c770d6a3ab8228adab55.png)
[groovy] mop meta object protocol and meta programming (use groovy meta programming for function interception | dynamic interception function | dynamically obtain methods in metaclass | evaluate metho

A quietly rising domestic software

无人驾驶虚拟仿真(十三)--图像处理之交通标志牌识别1

pytest 自动化测试框架(二)

如何关闭VS Code eslint校验,快来看看吧

基本功:SQL 多表联合查询的几种方式
随机推荐
Personal summary of three simple sorting for beginners
C language branch and loop statements
答应我, 不要再用 if (obj = null) 判空了
1个需求的一生,团队协作在云效钉钉小程序上可以这么玩
面了个腾讯拿 38K 出来的,让我见识到了基础的天花板
源码级别的广播与监听实现
虫子 归并 计数
Vous permet de jouer facilement avec le langage C scanf et getchar
虫子 二叉树OJ
53.最大子数组和
Tcpdump packet capture and nmap are easy to use
H5性能分析实战来啦~
I took out 38K from Tencent and showed me the basic ceiling
快速排序几种实现方法及其优化
Basic use of CEPH
一改测试步骤代码就全写?为什么不试试用 Yaml实现数据驱动?
A quietly rising domestic software
虫子 时空复杂度
Monotonicity and concavity of function
Understanding of machine learning