当前位置:网站首页>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;
}
}
边栏推荐
- Data center table reports realize customized statistics, overtime leave summary record sharing
- Leetcode 46 Full Permutation
- Bit. Store: long bear market, stable stacking products may become the main theme
- Byte + Google super full kotlin learning King fried notes! Kotlin introductory tutorial + Advanced kotlin enhanced actual combat (with demo)
- EMQ helps Qingdao Yanbo build a smart water platform
- About how vs2019c # establishes the login interface, the user name and password entered must match the records in the access database
- Leetcode daily practice (sum of two numbers)
- 印象深刻的问题
- 华为云DevCloud重磅发布四大新能力,创下国内两项第一
- 事务的隔离级别详解
猜你喜欢

Annual comprehensive analysis of China's audio market in 2022

Hung - Mung! HDD Hangzhou station · salon hors ligne vous invite à construire l'écologie

Defiato is an innovation that combines user-friendly features of a centralized platform with defi services

leetcode 200. Number of islands
P.A.R.A 方法在思源的简易应用(亲测好用)

Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)
A large number of missing anchor text

d3dx9_ What if 27.dll is lost? d3dx9_ How to repair 27.dll?

Bit. Store: long bear market, stable stacking products may become the main theme

Autodesk Navisworks 2022软件安装包下载及安装教程
随机推荐
特殊函数计算器
Deeply digitise, lead cloud nativity and serve more developers
Etcd可视化工具:Kstone部署(一),基于Helm快速部署
localDateTime类型的时间(2019-11-19T15:16:17) 用oracle的时间范围查询
2/14 preliminary calculation geometry
A robot is located in the upper left corner of an M x n grid. The robot can only move down or right one step at a time. The robot attempts to reach the lower right corner of the grid. How many differe
P.A.R.A 方法在思源的简易应用(亲测好用)
Oracle概念二
Handling of difficult and miscellaneous problems during the installation and configuration of qt5.5.1 desktop version (configuring arm compilation Kit)
d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll
National food safety risk assessment center: do not blindly and unilaterally pursue "zero addition" and "pure natural" food
d3dx9_ How to repair 38.dll? d3dx9_ 38. How to download a missing DLL?
模拟进程调度
New method of cross domain image measurement style relevance: paper interpretation and code practice
Community sharing jumpserver in the eyes of senior open source users: a fortress machine for "Crazy" iteration
Adaoracle supports multi chain distributed Oracle with wide area node quotation
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
Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)
如何提升IT电子设备效能管理
Defiato is an innovation that combines user-friendly features of a centralized platform with defi services