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

边栏推荐
- Account management of MySQL
- Introduction to Robotics II. Forward kinematics, MDH method
- 蓝桥杯单片机省赛第十二届第一场
- [ibdfe] matlab simulation of frequency domain equalization based on ibdfe
- Jetpack's livedata extension mediatorlivedata
- Which product of anti-cancer insurance is better?
- Suggestions on settlement solution of u standard contract position explosion
- [wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
- Go branch and loop
- 【小技巧】使用matlab GUI以对话框模式读取文件
猜你喜欢

Account management of MySQL

Opencv learning example code 3.2.4 LUT

Blue Bridge Cup single chip microcomputer sixth temperature recorder

蓝桥杯单片机省赛第十一届第二场

Basic syntax of unity script (6) - specific folder

【IBDFE】基于IBDFE的频域均衡matlab仿真

The 10th Blue Bridge Cup single chip microcomputer provincial competition

软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
![[ibdfe] matlab simulation of frequency domain equalization based on ibdfe](/img/a1/441f400668e736b70cb36443f2267a.png)
[ibdfe] matlab simulation of frequency domain equalization based on ibdfe

In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
随机推荐
NLog use
《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
SQL: common SQL commands
Monkey测试
蓝桥杯单片机省赛第九届
初识P4语言
2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有
Oracle 常用SQL
Which product of anti-cancer insurance is better?
Go variables and constants
Visual slam Lecture 3 -- Lie groups and Lie Algebras
QT designer plug-in implementation of QT plug-in
Suggestions on settlement solution of u standard contract position explosion
Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
The original author is out! Faker. JS has been controlled by the community..
Object oriented thinking
The 10th Blue Bridge Cup single chip microcomputer provincial competition
L'avènement de l'ère 5G, une brève discussion sur la vie passée et présente des communications mobiles
Didi open source Delta: AI developers can easily train natural language models