当前位置:网站首页>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
边栏推荐
猜你喜欢

Yarn resource scheduler

一文看懂 LSTM(Long Short-Term Memory)

基于PyQt5实现弹出任务进度条功能示例

【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)

《Redis设计与实现》

Zhengda international explains what the Dow Jones industrial index is?
MapReduce core and foundation demo
![Jerry's more accurate determination of abnormal address [chapter]](/img/ed/a08949bfc63823baf25fd57c324996.png)
Jerry's more accurate determination of abnormal address [chapter]

Windows installs redis and sets the redis service to start automatically

Computer network security experiment II DNS protocol vulnerability utilization experiment
随机推荐
通过流式数据集成实现数据价值(5)- 流处理
Longest common front string
Redis design and Implementation
正大国际讲解道琼斯工业指数到底是什么?
Realize data value through streaming data integration (3) - real-time continuous data collection
Configuration of LNMP
Realize data value through streaming data integration (1)
LeetCode 1249. Minimum remove to make valid parents - FB high frequency question 1
杰理之AES能256bit吗【篇】
解决方案架构师的小锦囊 - 架构图的 5 种类型
LeetCode-608. 树节点
art-template 模板引擎
Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
Reading integrity monitoring techniques for vision navigation systems - 3 background
Pyqt5 and communication
链表相交(链表)
通过流式数据集成实现数据价值(3)- 实时持续数据收集
NEC infrared remote control coding description
Turn: Maugham: reading is a portable refuge
域名和IP地址的联系