当前位置:网站首页>Mathematics to solve the problem - circular linked list
Mathematics to solve the problem - circular linked list
2022-07-31 02:20:00 【Chen Yikang】
Circular linked list

Things:
- First traverse the linked list through the fast and slow double pointers until the fast and slow meet and stop (The speed of fast must be twice the speed of slow, not three or four times... Imagine if the ring has only two knotsPoint, the fast must be advanced in the ring, if the slow in the backward ring is just staggered from the fast, the slow takes three times, and the fast takes two steps, and they will never meet!)
- Let one pointer be traversed from the head node, and the other pointer will be traversed from the point where the speed and slow pointers met last time, The traversal speed of the two pointers is the same and they will meet at the node entrance of the loop, I can't think of it?The explanation is as follows:

The code is as follows:
public class Solution {public ListNode detectCycle(ListNode head) {//fast pointer must be twice as fast as slow pointerListNode fast = head;ListNode slow = head;while(fast != null && fast.next != null){fast = fast.next.next;slow = slow.next;if(fast == slow){break;}}if(fast == null || fast.next == null){return null;}slow = head;while(slow != fast){slow = slow.next;fast = fast.next;}return slow;}}边栏推荐
- keep-alive cache component
- 真正的CTO,是一个懂产品的技术人
- CV-Model【3】:MobileNet v2
- Drools基本介绍,入门案例,基本语法
- 1. Non-type template parameters 2. Specialization of templates 3. Explanation of inheritance
- Force buckled brush the stairs (7/30)
- Is there a way to earn 300 yuan a day by doing a side business?
- The final exam first year course
- 【AcWing 62nd Weekly Game】
- 两个有序数组间相加和的Topk问题
猜你喜欢

基于FPGA的售货机

Are you still working hard on the limit of MySQL paging?

经典链表OJ强训题——快慢双指针高效解法

Static route analysis (the longest mask matching principle + active and standby routes)

There is a problem with the multiplayer-hlap package and the solution cannot be upgraded
![CV-Model [3]: MobileNet v2](/img/c7/1155a1f610110724c67a3b7557ef28.jpg)
CV-Model [3]: MobileNet v2

User interaction + formatted output

After reading "MySQL Database Advanced Practice" (SQL Xiao Xuzhu)

Crypto Life, a day in the life of a Web3 project partner

Teach you how to configure Jenkins automated email notifications
随机推荐
二层广播风暴(产生原因+判断+解决)
mmdetection trains a model related command
力扣刷题之有效的正方形(每日一题7/29)
1. Non-type template parameters 2. Specialization of templates 3. Explanation of inheritance
mysql view
rpm install postgresql12
How to do a startup CTO?
【AcWing 62nd Weekly Game】
tcp框架需要解决的问题
12张图带你彻底搞懂服务限流、熔断、降级、雪崩
Verify the integer input
Software testing basic interface testing - getting started with Jmeter, you should pay attention to these things
Path and the largest
Brute Force/Adjacency Matrix Breadth First Directed Weighted Graph Undirected Weighted Graph
全流程调度——MySQL与Sqoop
leetcode-399: division evaluation
Drools WorkBench的简介与使用
12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche
【银行系列第一期】中国人民银行
16. Registration Center-consul