当前位置:网站首页>leetcode 142. Circular linked list II
leetcode 142. Circular linked list II
2022-06-27 16:59:00 【chenyson】
difficulty : secondary
The frequency of :114
subject : Given the head node of a linked list head , Return to the first node of the link where the list begins to enter . If the list has no links , Then return to null.
If there is a node in the linked list , It can be done by continuously tracking next The pointer reaches again , Then there is a ring in the linked list . To represent a ring in a given list , The evaluation system uses an integer 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 . Be careful :pos Not passed as an argument , Just to identify the actual situation of the linked list .

Their thinking : Speed pointer
Be careful :
- law : The length from the node where the fast and slow pointers meet to the starting node of the ring =head The length to the ring start node 【 Use the slow pointer to walk , A from head Start , A place where the fast and slow pointers meet , Finally, the index is 0 The nodes meet 】
- Practice the circular linked list 141 while Don't forget in your judgment fast.next!=null
Code :
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast=head,slow=head;
//fast.next!=null Can't forget If you take two steps, you may encounter the next step and there is no way to go
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow) {
fast=head;
while(fast!=slow){
fast=fast.next;
slow=slow.next;
}
return slow;
}
}
return null;
}
}
边栏推荐
- What are the password requirements for waiting insurance 2.0? What are the legal bases?
- leetcode 200. Number of islands
- Handling method of occasional error reporting on overseas equipment
- Drawing for example study of flashcc
- Mihayou sued Minmetals trust, which was exposed to product thunderstorms
- New method of cross domain image measurement style relevance: paper interpretation and code practice
- Detailed explanation of transaction isolation level
- Handwritten promise series - all
- d3dx9_ What if 27.dll is lost? d3dx9_ How to repair 27.dll?
- IDE Eval reset unlimited trial reset
猜你喜欢

Handling method of occasional error reporting on overseas equipment

当发布/订阅模式遇上.NET

IDE Eval reset unlimited trial reset

Use pyinstaller to package py files into exe. Precautions and error typeerror:_ get_ sysconfigdata_ name() missing 1...‘ check_ Solutions to exists'
![[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth](/img/70/fa79ba38e28c41ed28bce2ec73cd79.png)
[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth

鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態

关于#mysql#的问题:问题遇到的现象和发生背景

Determine the maximum number of specific words in a string

Oracle concept 3

Mihayou sued Minmetals trust, which was exposed to product thunderstorms
随机推荐
Some details of Huawei OSPF
Leetcode daily practice (main elements)
SQLite net (SQLite is used by unity, WPF and WinForm)
Cesium realizes satellite orbit detour
d3dx9_ How to repair 32.dll? d3dx9_ Solution to 32.dll missing
How to improve it electronic equipment performance management
EMQ helps Qingdao Yanbo build a smart water platform
Ten common methods of arrays tools
09 route guard authenticates URL
Yyds dry inventory brief chrome V8 engine garbage collection
数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】
Why should string be designed to be immutable?
P.A.R.A 方法在思源的简易应用(亲测好用)
郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le
Oracle概念三
LACP details
d3dx9_ How to repair 25.dll? d3dx9_ 25.dll where to download
What is the level 3 password complexity of ISO? How often is it replaced?