当前位置:网站首页>leetcode 142. Circular linked list II
leetcode 142. Circular linked list II
2022-04-21 07:47:00 【Konjaku in Grade 5 of primary school】
leetcode 142. Circular list Ⅱ
Hello everyone , I'm studying konjak in grade five of primary school , Focus on the back end , Witness the growth of konjaku together , Your comments, praise and attention are my biggest motivation , If you have any mistakes, please let me know , Thank you very much . Let's support originality ! There is a mistake in writing .
Title Description
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 .
Answer key
Ideas :
Use the speed pointer , The fast pointer moves two at a time, and the slow pointer moves one at a time , after n After the circle, we will meet , When meeting, because the fast pointer moves at a time, the two slow pointers move one at a time , So the fast pointer moves twice as many nodes as the slow pointer . The entry node of the ring linked list is the number of nodes left in the slow pointer unit circle , Another pointer can be used to start from the node , When the loop meets the slow pointer, it can get the entry node
/* * @lc app=leetcode.cn id=142 lang=cpp * * [142] Circular list II */
// @lc code=start
/** * 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 *slow, *fast, *p;
slow = fast = p = head;
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
// Ring
if (slow == fast)
{
while (p != slow)
{
p = p->next;
slow = slow->next;
}
return p;
}
}
// Non ring
return nullptr;
}
};
// @lc code=end
版权声明
本文为[Konjaku in Grade 5 of primary school]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210626569140.html
边栏推荐
猜你喜欢

GoLang学习资源清单

论文阅读:Analyzing Third Party Service Dependencies in Modern Web Services

Leetcode 1423.可获得的最大点数(Maximum Points You Can Obtain from Cards)

关于数据治理平台中数据仓库ODS、DW和DM概念理解
![Navicat连Oracle报错[No matching authentication protocol]](/img/16/4dd115fc5abc68f7d1ffa640165fd9.png)
Navicat连Oracle报错[No matching authentication protocol]

MySQL数据库运行代码后,中文显示的是问号 ?

pycharm 最新导入PIL库的方法

记录使用fastjson消息转换器遇到的问题和解决方法

服务器部署svn环境

Flutter初体验
随机推荐
pycharm 最新导入PIL库的方法
.net core 部署至Linux平台上访问SQL SERVER 2008 R2时提示连接超时异常
GeoServer 2.20.1 解决跨域问题 CORS
Leetcode 1387.将整数按权重排序(Sort Integers by The Power Value)
应用软件线程数与CPU核数之间的关系
Leetcode 704 · binary search
批处理解析域名的ip地址并打开网页
SHA256 Hashes
Regular Expressions
oracle 管理之《sql命令》
When deploying. Net core on Linux platform to access SQL Server 2008 R2, it prompts the connection timeout exception
leetcode 206.反转链表
SHA256 Hashes
虚拟机安装kali 时的默认密码(官网vmx 文件源)
c#生成中文金额并用语音读取出来的完整类
SQLServer的3种分页查询方式sql2000/2008/2012的
URL Parsing
Environment Variables
压缩毕业时间到20岁以前,5岁上小学上5年,提高人口数量
UMLet初学者使用步骤