当前位置:网站首页>[force buckle] ring list II
[force buckle] ring list II
2022-07-19 06:25:00 【Patrick star`】
142. Circular list II - Power button (LeetCode)
subject :
Given the head node of a linked list head , Return to the first node of the link where the list begins to enter . If the list has no links , Then return to null.
Ideas :
Use the speed pointer

Code :
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode* fast = head;
struct ListNode* slow = head;
while(fast && fast->next)
{
fast = fast->next->next;
slow = slow->next;
if(slow == fast)
{
struct ListNode* temp = head;
while(1)
{
if(temp == slow)
{
return slow;
}
temp=temp->next;
slow=slow->next;
}
}
}
return NULL;
}expand :
1、slow One go 1 Step ,fast One go 2 Step , Can you catch up with me ?
answer : You can definitely make it , hypothesis slow Enter the cycle and fast The distance to N,fast Two steps at a time ,slow One step at a time , Every time fast and slow The distance will -1,N Next time ,fast and slow The distance is 0.
2、slow One go 1 Step ,fast One go 3 Step , Can you catch up ?fast One go 4 Step ,n Step ?
answer : not always , hypothesis slow Enter the cycle and fast The distance to N, every time fast and slow The distance will -2, If N If it is not a multiple of two ,fast and slow The distance cannot be 0.
3、 Prove the above method of finding the circulation entrance .

Set the distance before entering the cycle as l
The entrance to fast and slow The distance of the meeting point is x
The length of one cycle is c
slow The length traveled : l+x
fast The length traveled : l + nc+x
fast The walking length is slow Of 2 times
2(l+x) = I+x +nc
l = nc -x
l = (n-1)*c + c-x
therefore l = c-x
4、 Other solutions

When fast and slow After encounter , Disconnect the cycle , It becomes the problem of finding the intersection of two linked lists
边栏推荐
- 浅谈跨域的几种解决方案
- LeetCode树
- EOG-based eye movement detection and gaze estimation for an asynchronous virtual keyboard基于EOG的异步虚
- Peerless good problem (bit operation optimization DP)
- 2022 RoboCom 世界机器人开发者大赛-本科组(省赛)
- uboot 编译前的配置命令make config分析
- 【力扣】翻转二叉树
- 自动补全 & (自定义)拼音分词器 & 搜索时注意事项
- MEX and Increments
- Introduction to basic knowledge of Minio
猜你喜欢

EOG-based eye movement detection and gaze estimation for an asynchronous virtual keyboard基于EOG的异步虚

三维凝视估计,没有明确的个人校准2018

解决:无法加载文件 C:\Program Files\.. 因为在此系统上禁止运行脚本...

2022/07/09 第五小组 丁帅 学习笔记 day02

Loadng class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is `com.mysql.cj.jdb

Simple chrome script automatically skips the charging acknowledgment page after the video playback of station B ends

2022/07/12 学习笔记 (day05)循环

2021-09-15

软考初、中、高级考试全体验

Introduction to basic knowledge of Minio
随机推荐
Low power LDO linear regulator IC
Simple chrome script automatically skips the charging acknowledgment page after the video playback of station B ends
Key points of embedded C language (const, static, volatile, bit operation)
2022/07/10 第五小组 丁帅 学习笔记 day03
Where have all the older programmers gone?
计算几何(4.17)
MCU single chip OTP
Addition and subtraction of busybox date time
make menuconfig缺少ncurses
Solve cannot read properties of null (reading 'pickalgorithm')
filezilla传输虚拟机速度慢解决方法
Ehab the Xorcist (异或性质,构造)
【力扣】单值二叉树
Guess the string (dichotomy, interaction)
索引库中的文档的操作
Darwin reflex summary
Unity2D学习 Fox Game制作 过程1:基本的游戏角色控制,动画效果,镜头控制,物品收集,bug优化
Acwing第57场周赛(AK)
Cable TV network (tree grouping)
软考初、中、高级考试全体验