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

边栏推荐
- 【IBDFE】基于IBDFE的频域均衡matlab仿真
- Flutter中深入了解MaterialApp,常用属性解析
- Basic operations of MySQL database (based on tables)
- Failed to upgrade schema, error: “file does not exist
- Opencv learning example code 3.2.4 LUT
- C语言:逻辑运算和判断选择结构例题
- VS2010 plug-in nuget
- 接口调试工具模拟Post上传文件——ApiPost
- go 分支与循环
- The original author is out! Faker. JS has been controlled by the community..
猜你喜欢
![[personal notes] PHP common functions - custom functions](/img/3d/d50622e3ddb08f654f30063e8226ac.jpg)
[personal notes] PHP common functions - custom functions

Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface

Failed to upgrade schema, error: “file does not exist

Go language introduction

SQL Yiwen get window function

How should the team choose the feature branch development mode or trunk development mode?

Wpviewpdf Delphi and Net PDF viewing component
![[designmode] builder model](/img/e8/855934d57eb6868a4d188b2bb1d188.png)
[designmode] builder model

Qt插件之Qt Designer插件实现

蓝桥杯单片机省赛第十一届第一场
随机推荐
The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
Go language naming specification
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
Which product of anti-cancer insurance is better?
《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
Three ways for programmers to learn PHP easily and put chaos out of order
SQL Yiwen get window function
【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
regular expression
【leetcode】81. Search rotation sort array II
C language: examples of logical operation and judgment selection structure
Visual slam Lecture 3 -- Lie groups and Lie Algebras
毕设-基于SSM电影院购票系统
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
go 函数
JVM knowledge points
Where can I buy cancer insurance? Which product is better?
Blue Bridge Cup single chip microcomputer sixth temperature recorder
Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
【leetcode】34. Find the first and last positions of elements in a sorted array