当前位置:网站首页>[speed pointer] 142 circular linked list II
[speed pointer] 142 circular linked list II
2022-07-05 05:16:00 【lee2813】
One 、 subject
Given a linked list , Return to the first node of the link where the list begins to enter . If the list has no links , Then return to 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 , The evaluation system uses an integer 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 .
No modification allowed Linked list .
Example 1:
Input :head = [3,2,0,-4], pos = 1
Output : The return index is 1 The linked list node of
explain : There is a link in the list , Its tail is connected to the second node .
Example 2:

Input :head = [1,2], pos = 0
Output : The return index is 0 The linked list node of
explain : There is a link in the list , Its tail is connected to the first node .
Example 3:

Input :head = [1], pos = -1
Output : return null
explain : There are no links in the list .
Two 、 Answer key
For the link list to find the loop problem , There is a general solution —— Fast and slow pointer method :
Given that both pointers point to the starting point , One for fast pointer , Move two steps at a time ; One is a slow pointer , One step at a time . If the fast pointer can go to the end , Explain the Fifth Ring Road , Otherwise there is a loop .
After confirming that there is a loop , How to find the starting point of the loop ?
- First, when determining the loop , The end condition is that the speed pointer meets for the first time
- Then the fast pointer is reset to the starting point , Slow hands don't move , When moving the second time, the fast and slow pointer moves only one step each time , Until the second meeting , At this time, the point of meeting is the starting point of the loop .
3、 ... and 、 Code
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* fast = head;
ListNode* slow = head;
do{
if(!fast || !fast->next) return nullptr;
fast = fast->next->next;
slow = slow->next;
}while(fast!=slow);
fast = head;
while(fast!=slow){
fast = fast->next;
slow = slow->next;
}
return fast;
}
};
边栏推荐
- Unity find the coordinates of a point on the circle
- [LeetCode] 整数反转【7】
- 2020-10-27
- Es module and commonjs learning notes
- 被舆论盯上的蔚来,何时再次“起高楼”?
- Use the command character to close the keyboard command of the notebook
- Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area
- How much do you know about 3DMAX rendering skills and HDRI light sources? Dry goods sharing
- 3dsmax snaps to frozen objects
- Generate filled text and pictures
猜你喜欢

Stm32cubemx (8): RTC and RTC wake-up interrupt

【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research

Reverse one-way linked list of interview questions

Embedded database development programming (VI) -- C API

Unity check whether the two objects have obstacles by ray

Quick sort summary

C语言杂谈1
![[轉]: OSGI規範 深入淺出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[轉]: OSGI規範 深入淺出

Unity3d learning notes

National teacher qualification examination in the first half of 2022
随机推荐
Unity connects to the database
Page countdown
[转]:Apache Felix Framework配置属性
2022/7/1學習總結
Redis has four methods for checking big keys, which are necessary for optimization
被舆论盯上的蔚来,何时再次“起高楼”?
JVM call not used once in ten years
Unity3d learning notes
National teacher qualification examination in the first half of 2022
Unity writes timetables (without UI)
64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
Unity check whether the two objects have obstacles by ray
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
嵌入式数据库开发编程(五)——DQL
Insert sort
Use the command character to close the keyboard command of the notebook
Reverse one-way linked list of interview questions
FVP和Juno平台的Memory Layout介绍
Listview pull-down loading function
3dsmax snaps to frozen objects