当前位置:网站首页>[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;
}
};
边栏推荐
- 用 Jmeter 工具做个小型压力测试
- 《动手学深度学习》学习笔记
- 质量体系建设之路的分分合合
- FVP和Juno平台的Memory Layout介绍
- Recherche de mots pour leetcode (solution rétrospective)
- National teacher qualification examination in the first half of 2022
- China polyurethane rigid foam Market Research and investment forecast report (2022 Edition)
- How much do you know about 3DMAX rendering skills and HDRI light sources? Dry goods sharing
- Research and investment forecast report of adamantane industry in China (2022 Edition)
- [LeetCode] 整数反转【7】
猜你喜欢
Do a small pressure test with JMeter tool
Grail layout and double wing layout
质量体系建设之路的分分合合
PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low
GBase数据库助力湾区数字金融发展
2022年上半年国家教师资格证考试
Chinese notes of unit particle system particle effect
LeetCode之单词搜索(回溯法求解)
Merge sort
3dsmax snaps to frozen objects
随机推荐
Embedded database development programming (VI) -- C API
C # perspective following
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
2022/7/2 question summary
[turn]: Apache Felix framework configuration properties
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
Generate filled text and pictures
Embedded database development programming (zero)
Magnifying glass effect
A three-dimensional button
Personal required code
[trans]: spécification osgi
2022/7/2做题总结
FVP和Juno平台的Memory Layout介绍
Out and ref functions of unity
Embedded database development programming (V) -- DQL
Lua GBK and UTF8 turn to each other
Research and investment forecast report of adamantane industry in China (2022 Edition)
《动手学深度学习》学习笔记
Unity ugui source code graphic