当前位置:网站首页>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;
}
}
边栏推荐
- Raspberry pie preliminary use
- LACP details
- National food safety risk assessment center: do not blindly and unilaterally pursue "zero addition" and "pure natural" food
- Relation and operation of ORM table
- 字节跳动埋点数据流建设与治理实践
- Cesium realizes satellite orbit detour
- ROS "topic" programming implementation
- Julia constructs diagonal matrix
- Drawing for example study of flashcc
- Oracle概念二
猜你喜欢

Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology

数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】

Community sharing jumpserver in the eyes of senior open source users: a fortress machine for "Crazy" iteration

模拟进程调度

leetcode 200. Number of islands

Mobile terminal click penetration

IDE Eval reset unlimited trial reset

Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance

About MySQL: the phenomenon and background of the problem
A large number of missing anchor text
随机推荐
鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
LACP details
Annual comprehensive analysis of China's audio market in 2022
A large number of missing anchor text
C语言课程设计
Oracle概念三
What are the password requirements for waiting insurance 2.0? What are the legal bases?
2/15 topology sorting +dfs (the order of specified directions is very important) +bfs
The two trump brand products of Langjiu are resonating in Chengdu, continuously driving the consumption wave of bottled liquor
leetcode 200. Number of islands
Unity shadow shadow pancaking
Extract field year / quarter effect based on date
#yyds干货盘点# 解决剑指offer:二叉树中和为某一值的路径(三)
Pragma once Usage Summary
09 route guard authenticates URL
The European unified charging specification act was passed before the end of the year, and it is planned to expand to products such as laptop and keyboard
LeetCode每日一练(两数之和)
Missing d3d10 How to repair DLL files? Where can I download d3d10.dll
Yyds dry inventory brief chrome V8 engine garbage collection
Relation and operation of ORM table