当前位置:网站首页>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;
}
}
边栏推荐
- 如何提升IT电子设备效能管理
- Oracle concept II
- Huawei cloud devcloud launched four new capabilities, setting two domestic firsts
- Community sharing jumpserver in the eyes of senior open source users: a fortress machine for "Crazy" iteration
- Leetcode daily practice (sum of two numbers)
- LeetCode 124. Binary tree maximum path sum - binary tree series question 8
- Open source 23 things shardingsphere and database mesh have to say
- 等保2.0密码要求是什么?法律依据有哪些?
- wheel ui
- Extract field year / quarter effect based on date
猜你喜欢

Oracle概念二

07. Express routing

Domain name binding dynamic IP best practices

Missing d3d10 How to repair DLL files? Where can I download d3d10.dll

Annual comprehensive analysis of China's audio market in 2022

Data center table reports realize customized statistics, overtime leave summary record sharing

When the publish / subscribe mode encounters NET

Redis Series 2: data persistence improves availability

# Cesium实现卫星在轨绕行

IDE Eval reset unlimited trial reset
随机推荐
Solving Poisson equation by tensorflow
Determine the maximum number of specific words in a string
智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
d3dx9_ How to repair 25.dll? d3dx9_ 25.dll where to download
Synchronization mechanism of dual namenodes
LACP details
QT5 之信号与槽机制(信号与槽的基本介绍)
Some details of Huawei OSPF
2/14 preliminary calculation geometry
C语言课程设计
全面解析零知识证明:消解扩容难题 重新定义「隐私安全」
Deeply digitise, lead cloud nativity and serve more developers
Under the influence of external factors, how to manage internal resources and reduce costs is the key
Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
Data center table reports realize customized statistics, overtime leave summary record sharing
#yyds干货盘点#简述chromeV8引擎垃圾回收
National food safety risk assessment center: do not blindly and unilaterally pursue "zero addition" and "pure natural" food
10 minutes to master the installation steps of MySQL
Ten common methods of arrays tools
06. First introduction to express