当前位置:网站首页>142. Circular linked list||
142. Circular linked list||
2022-04-23 10:15:00 【Popuessing's Jersey】
The question : Given a linked list , Return to the first node of the link where the list begins to enter . If the list has no links , Then return to null.
To represent a ring in a given list , Use integers pos To indicate where the end of the list is connected to the list ( Index from 0 Start ). If pos yes -1, There are no links in the list .
Advanced challenges : It is not allowed to modify the given linked list .
Method : Speed pointer ( Double pointer )
public class Huanxinglianbiao2 {
public ListNode detectCycle(ListNode head){
// Set speed pointer
ListNode fast = head;
ListNode slow = head;
while(fast!=null && fast.next!=null){
// Let's go two steps
// Slow pointer one step
fast = fast.next.next;
slow = slow.next;
// If fast == slow Confirm that there is a ring
// Find the meeting point of the fast and slow pointers , Meet again. Come back to head, The slow pointer remains stationary
// They move forward at the same speed , When they meet again , It's the entry point of the circular entrance
if (fast == slow){
fast = head;
while (fast!=slow){
fast = fast.next;
slow = slow.next;
}
// Back to the entrance of the ring
return fast;
}
}
// End of traversing the linked list , Ring not found , It returns null ;
return null;
}
}
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231011140899.html
边栏推荐
- Sim Api User Guide(8)
- 第二章 In-Memory 体系结构 (IM-2.2)
- Yarn core parameter configuration
- Go language practice mode - functional options pattern
- 通过流式数据集成实现数据价值(3)- 实时持续数据收集
- Failureforwardurl and failureurl
- Redis design and Implementation
- lnmp的配置
- Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
- 349、两个数组的交集
猜你喜欢
JUC concurrent programming 09 -- source code analysis of condition implementation
The central control learning infrared remote control module supports network and serial port control
Juc并发编程07——公平锁真的公平吗(源码剖析)
2022年流动式起重机司机考试题库模拟考试平台操作
JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
Arm debugging (1): two methods to redirect printf to serial port in keil
Custom login failure handling
2022茶艺师(初级)考试试题模拟考试平台操作
Yarn core parameter configuration
第120章 SQL函数 ROUND
随机推荐
Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
使用IDEA开发Spark程序
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
一文读懂PlatoFarm新经济模型以及生态进展
DBA常用SQL语句 (5) - Latch 相关
Read LSTM (long short term memory)
art-template 模板引擎
SQL tuning series - Introduction to SQL tuning
Function realization of printing page
DBA常用SQL语句(2)— SGA和PGA
Sim Api User Guide(5)
0704、ansible----01
ARM调试(1):两种在keil中实现printf重定向到串口的方法
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
DBA common SQL statements (5) - latch related
Realize data value through streaming data integration (1)
SQL tuning series - SQL performance methodology
202、快乐数
LeetCode-608. Tree node