当前位置:网站首页>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
边栏推荐
- Realize data value through streaming data integration (2)
- 0704、ansible----01
- MapReduce计算流程详解
- Sim Api User Guide(6)
- 利用多线程按顺序连续输出abc10次
- 242、有效字母异位词(哈希表)
- Chapter 2 Oracle database in memory architecture (I) (im-2.1)
- Chapter 3 enable and adjust the size of IM column storage (im-3.1)
- Redis design and Implementation
- 定义链表(链表)
猜你喜欢

The central control learning infrared remote control module supports network and serial port control

101. Symmetric Tree

【无标题】

JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer

Sim Api User Guide(6)

Solution architect's small bag - 5 types of architecture diagrams

Yarn core parameter configuration
MapReduce核心和基础Demo

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

Operation of 2022 tea artist (primary) test question simulation test platform
随机推荐
What are Jerry's usual program exceptions? [chapter]
What are the system events of Jerry's [chapter]
Question bank and answers of Shanghai safety officer C certificate examination in 2022
Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]
Art template template engine
Odoo server setup notes
杰理之通常程序异常情况有哪些?【篇】
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
第二章 Oracle Database In-Memory 体系结构(上) (IM-2.1)
一文读懂PlatoFarm新经济模型以及生态进展
二叉树的构建和遍历
"Gu Yu series" airdrop
Chapter I Oracle database in memory related concepts (Continued) (im-1.2)
DBA common SQL statements (2) - SGA and PGA
SQL tuning series - Introduction to SQL tuning
Go language practice mode - functional options pattern
Depth selector
Sim Api User Guide(4)
Sim Api User Guide(8)
JUC concurrent programming 09 -- source code analysis of condition implementation