当前位置:网站首页>Circular linked list 2
Circular linked list 2
2022-04-22 05:42:00 【Xiao He】
Continuously update the force buckle , Two solutions to each problem
A quick solution , A kind of brain shell solution
Catalog
Answer key 2: Fast and slow pointer to find the law
Title Description
Topic link :
https://leetcode-cn.com/problems/linked-list-cycle-ii/
subject :

Example : requirement : Return to node 2

Answer key 1: Use set
Thought is to use ArrayList<> aggregate , Store the linked list nodes into the set one by one
If the list has links , As shown in the example ,head End of traversal -4 after , To traverse 2, Judged 2 Already stored in the collection , There are links in the list
Icon :

public class Solution {
public ListNode detectCycle(ListNode head) {
// If there are no nodes , Or just one node , There can be no ring
if (head == null || head.next == null) return null;
ListNode cur = head;
// Use extra space
ArrayList<ListNode> list = new ArrayList<ListNode>();
while (cur.next != null) {
// Store the node at this time in the collection
list.add(cur);
// Pointer backward
cur = cur.next;
// If there are already stored in the collection cur, Then return it
if (list.contains(cur)) {
return cur;
}
}
// Loop exit means cur It's empty , There are no links in the linked list
return null;
}
}
Answer key 2: Fast and slow pointer to find the law
Seeing the speed pointer, did you think of “ Circular list 1” This question ? But these two questions are different , The position where the fast and slow pointers meet is uncertain , As the number of nodes changes , The place of meeting is also different

So how to find the starting point of the ring ? This requires a little mathematical knowledge
As shown in the figure , Starting from P, The intersection of the linked list and the ring is Q, The fast pointer and the slow pointer first met in R

hypothesis
PQ Long for x QR Long for y
The circumference of the ring is c
Slow the pointer to R Let's go at two o'clock :
L slow = x + λc + y; (λ Indicates the number of turns of slow pointer movement , As a positive integer )
L block = x + βc + y;①(β Indicates the number of turns of the fast pointer , As a positive integer )
Because the pointer takes two steps , Slow pointer takes one step , therefore L fast = 2 * L slow
namely :L fast = 2 * (x + λc + y);②
① And ② All represent the distance the fast pointer moves , The two formulas are equal
x + βc + y = 2 * (x + λc + y)
Simplify :x + y = (β - 2*λ)c
because β and λ Are integers. , therefore ( β - 2*λ )* c Is the integer circle of a ring , At this point, the answer has come out , If you don't understand , We continue :
hypothesis ( β - 2*λ ) yes 3,
and x + y The journey is Cyclic 3 circle namely :x + y = 3c
At this point, the fast pointer and the slow pointer meet , Let's stop them
Define another pointer p, Let it move from the beginning , One node at a time , When he moves to Q a.m. ,p The journey is x
Then the pointer goes slowly ( Two laps + c - y),c - y How much is? ?
If you don't understand , We can bring it in ,

Here is the code :
public class Solution {
public ListNode detectCycle(ListNode head) {
// If there is no node or only one node , There must be no ring , Go straight back to
if (head == null || head.next == null) return null;
// Speed pointer
ListNode fast = head;
ListNode slow = head;
while (true) {
// Prevent null pointer exception , stay “ Circular list 1” Said in
if (fast.next == null || fast.next.next == null)
return null;
// Go two steps at a time
fast = fast.next.next;
// Slow pointer one step at a time
slow = slow.next;
// Fast and slow hands meet , stop
if (fast == slow) {
break;
}
}
// Define a pointer p Run with the slow pointer , Both hands reach the starting point of the ring at the same time
ListNode p = head;
while (p != slow) {
p = p.next;
slow = slow.next;
}
return p;
}
}
版权声明
本文为[Xiao He]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220532004600.html
边栏推荐
猜你喜欢

Read write separation of MYCAT

Domain based approach - score prediction

Force buckle - 300 Longest increasing subsequence

Force buckle 876 Intermediate node of linked list

AcWing 836. 合并集合(并查集)

How to use on duplicate key update in MySQL

JVM探究

MySQL数据库基础

IDEA的安裝与使用技巧

2022 Niuke winter vacation supplementary question record 2
随机推荐
判断链表是否有环
SQL优化最佳实践
MySQL函数及练习题(二)
环形链表2
Learning notes - longest subsequence, largest sub block
opencv 使用 forEach 像素遍历(pixel 和 const int* position参数都有介绍)
codeforces div2 777 A
Mysql基础知识
MySQL Chapter 6 installation and use of Navicat
@PostConstruct方法内部死循环引起的问题
线性筛法(素数筛)
List stream: usage instance of reduce
Force buckle 876 Intermediate node of linked list
MySQL index
Domain based approach - score prediction
Optional最佳实践
IDEA debug高级操作
ENUM et expressions lambda
ThreadLocal.ThreadLocalMap分析
mysql中on duplicate key update 使用详解