当前位置:网站首页>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;
}
}

边栏推荐
- 蓝桥杯单片机省赛第十届
- go 函数
- [untitled]
- 高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
- 【leetcode】34. Find the first and last positions of elements in a sorted array
- Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
- Déchirure à la main - tri
- 树莓派GPIO引脚控制红绿灯与轰鸣器
- First acquaintance with string+ simple usage (II)
- 蓝桥杯单片机省赛第十二届第一场
猜你喜欢

Wpviewpdf Delphi and Net PDF viewing component

The 11th Blue Bridge Cup single chip microcomputer provincial competition
![[Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)](/img/5e/81e613370c808c63665c14298f9a39.png)
[Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)

Jetpack之LiveData扩展MediatorLiveData

Blue Bridge Cup single chip microcomputer sixth temperature recorder
![[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

【小技巧】使用matlab GUI以对话框模式读取文件

The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup

The 7th Blue Bridge Cup single chip microcomputer provincial competition

【leetcode】34. Find the first and last positions of elements in a sorted array
随机推荐
Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
手撕——排序
Jetpack's livedata extension mediatorlivedata
Hand tear - sort
蓝桥杯单片机省赛第十届
潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
Visual slam Lecture 3 -- Lie groups and Lie Algebras
JVM知识点
[designmode] Prototype Pattern
Welcome the winter vacation multi school league game 2 partial solution (B, C, D, F, G, H)
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
The 9th Blue Bridge Cup single chip microcomputer provincial competition
Where can I buy cancer insurance? Which product is better?
go 函数
Account management of MySQL
The 11th Blue Bridge Cup single chip microcomputer provincial competition
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
XSS prevention
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
Class design basis and advanced