当前位置:网站首页>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;
}
}
边栏推荐
- The 9th Blue Bridge Cup single chip microcomputer provincial competition
- 整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
- The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
- Fingertips life Chapter 4 modules and packages
- 跳出舒适区,5年点工转型自动化测试工程师,我只用了3个月时间
- go 函数
- Vite: scaffold assembly
- 蓝桥杯单片机省赛第十一届
- Account management of MySQL
- 2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有
猜你喜欢
Failed to upgrade schema, error: “file does not exist
傅里叶级数
The 7th Blue Bridge Cup single chip microcomputer provincial competition
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
《动手学深度学习》(二)-- 多层感知机
初识P4语言
BiShe cinema ticket purchasing system based on SSM
蓝桥杯单片机省赛第十一届
[designmode] builder model
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
随机推荐
Didi open source Delta: AI developers can easily train natural language models
Network connection mode of QT
PR zero foundation introductory guide note 2
Qt插件之Qt Designer插件实现
藍湖的安裝及使用
Hands on deep learning (II) -- multi layer perceptron
文档声明与字符编码
Monkey测试
潘多拉 IOT 开发板学习(HAL 库)—— 实验2 蜂鸣器实验(学习笔记)
What kind of interview is more effective?
蓝桥杯单片机省赛第九届
初识string+简单用法(二)
整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
The 11th Blue Bridge Cup single chip microcomputer provincial competition
Influence of air resistance on the trajectory of table tennis
【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)
Opencv learning example code 3.2.4 LUT
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
Oracle viewing locked tables and unlocking
【leetcode】81. Search rotation sort array II