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

边栏推荐
- How to solve the code error when storing array data into the database
- Blue Bridge Cup SCM digital tube skills
- 蓝桥杯单片机省赛第十届
- 文档声明与字符编码
- The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
- [tips] use Matlab GUI to read files in dialog mode
- Three ways for programmers to learn PHP easily and put chaos out of order
- 软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
- 蓝桥杯单片机数码管技巧
- Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
猜你喜欢

一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)

Basic syntax of unity script (6) - specific folder

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

Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
![[ibdfe] matlab simulation of frequency domain equalization based on ibdfe](/img/a1/441f400668e736b70cb36443f2267a.png)
[ibdfe] matlab simulation of frequency domain equalization based on ibdfe

Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions

Sorted out an ECS summer money saving secret, this time @ old users come and take it away

2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee

The 8th Blue Bridge Cup single chip microcomputer provincial competition
![[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
随机推荐
Blue Bridge Cup SCM digital tube skills
u本位合约爆仓清算解决方案建议
SQL Yiwen get window function
C language: examples of logical operation and judgment selection structure
Basic syntax of unity script (8) - collaborative program and destruction method
蓝桥杯单片机数码管技巧
PR zero foundation introductory guide note 2
接口调试工具模拟Post上传文件——ApiPost
蓝桥杯单片机省赛第十二届第一场
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
Wpviewpdf Delphi and Net PDF viewing component
Uni app - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
Oracle common SQL
Basic operations of MySQL database (based on tables)
What kind of interview is more effective?
XSS prevention
Flutter中深入了解MaterialApp,常用属性解析
初识P4语言
[designmode] builder model
Network connection mode of QT