当前位置:网站首页>Li Kou interview question 02.08 Loop detection
Li Kou interview question 02.08 Loop detection
2022-07-02 03:58:00 【Ruthless young Fisherman】
subject
Given a linked list , If it is a linked list , Implement an algorithm to return to the beginning node of the loop . If the ring does not exist , Please return 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 , We use integers 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 .
Example

Input :head = [3,2,0,-4], pos = 1
Output :tail connects to node index 1
explain : There is a link in the list , Its tail is connected to the second node .

Input :head = [1,2], pos = 0
Output :tail connects to node index 0
explain : There is a link in the list , Its tail is connected to the first node .

Input :head = [1], pos = -1
Output :no cycle
explain : There are no links in the list .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/linked-list-cycle-lcci
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method 1: Speed pointer
1、 Find the meeting point first 
2、 Find the starting point of the ring 
Java Realization
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
// Find a meeting point
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) break;
}
if (fast == null || fast.next == null) return null;
// Starting point of ring finding
slow = head;
while (fast != slow) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
}

边栏推荐
- Imageai installation
- 集成底座方案演示说明
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
- Flutter中深入了解MaterialApp,常用属性解析
- 傅里叶级数
- The 6th Blue Bridge Cup single chip microcomputer provincial competition
- 5g era is coming in an all-round way, talking about the past and present life of mobile communication
- MD5 of Oracle
- What kind of interview is more effective?
- 蓝桥杯单片机省赛第九届
猜你喜欢

手撕——排序

"No war on the Western Front" we just began to love life, but we had to shoot at everything

潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)

Déchirure à la main - tri

Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
![[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network](/img/11/4a8b52603e6e14a1ed6da1264dee57.png)
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network

Wpviewpdf Delphi and Net PDF viewing component
![[personal notes] PHP common functions - custom functions](/img/3d/d50622e3ddb08f654f30063e8226ac.jpg)
[personal notes] PHP common functions - custom functions

Installation and use of blue lake

Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
随机推荐
《动手学深度学习》(二)-- 多层感知机
蓝桥杯单片机省赛第十二届第一场
【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)
Introduction to Robotics II. Forward kinematics, MDH method
文档声明与字符编码
The 10th Blue Bridge Cup single chip microcomputer provincial competition
Qt插件之Qt Designer插件实现
Lost a few hairs, and finally learned - graph traversal -dfs and BFS
蓝湖的安装及使用
Class design basis and advanced
树莓派GPIO引脚控制红绿灯与轰鸣器
Raspberry pie GPIO pin controls traffic light and buzzer
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
Go language introduction
Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
接口调试工具模拟Post上传文件——ApiPost
Didi open source Delta: AI developers can easily train natural language models
一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)
Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini