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

边栏推荐
- Hands on deep learning (II) -- multi layer perceptron
- 文档声明与字符编码
- [wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
- Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
- Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
- [ibdfe] matlab simulation of frequency domain equalization based on ibdfe
- Go语言介绍
- u本位合约爆仓清算解决方案建议
- Suggestions on settlement solution of u standard contract position explosion
猜你喜欢

Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)

近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下

Basic operations of MySQL database (based on tables)

PR zero foundation introductory guide note 2

The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition

蓝桥杯单片机省赛第八届

u本位合约爆仓清算解决方案建议
![[personal notes] PHP common functions - custom functions](/img/3d/d50622e3ddb08f654f30063e8226ac.jpg)
[personal notes] PHP common functions - custom functions

【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证

The 9th Blue Bridge Cup single chip microcomputer provincial competition
随机推荐
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
Account management of MySQL
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
藍湖的安裝及使用
Basic syntax of unity script (6) - specific folder
蓝桥杯单片机省赛第六届
SQL: common SQL commands
JVM知识点
Imageai installation
How about Ping An lifetime cancer insurance?
The 5th Blue Bridge Cup single chip microcomputer provincial competition
The 10th Blue Bridge Cup single chip microcomputer provincial competition
[untitled]
蓝桥杯单片机省赛第八届
初识P4语言
Didi open source Delta: AI developers can easily train natural language models
[designmode] builder model
Welcome the winter vacation multi school league game 2 partial solution (B, C, D, F, G, H)
Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
Lost a few hairs, and finally learned - graph traversal -dfs and BFS