Leetcode - 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
分析:
假设这个链表如图所示,链表长度为L,相遇点是6节点,起始点到环开始点距离为a,环开始点到相遇点距离为x。设慢指针速度为1,快指针速度为2,相遇时间为t,则有:
t = a + x ............(1)
2t = L + x............(2)(2) - (1) 得:
t = L - a.............(3)
(3) - (1) 得:
a = L - a - x
更通用的算法应该是快指针绕了n(n>=1)圈后与慢指针相遇,但是为了理解方便就用图中的例子。
可以得出相遇时,链表头指针距环开始点的距离等于相遇点逆时针方向距环开始点的距离。那我们在相遇时,再用一个慢指针从链表头指针出发,一定可以和前一个慢指针在环开始点相遇。
Solution:
/*** 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 = head;ListNode* fast = head;while(fast && fast->next){slow = slow->next;fast = fast->next->next;if(slow == fast){ListNode* slow2 = head;while(slow != slow2){slow = slow->next;slow2 = slow2->next;}return slow;}}return nullptr;}
};
Leetcode - 142. Linked List Cycle II相关推荐
- Leetcode 142. Linked List Cycle II
地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...
- 【To Do】LeetCode 142. Linked List Cycle II
LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...
- Leetcode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 题意:找 ...
- LeetCode 142. Linked List Cycle II--单向链表成环的起点--C++,Python解法
题目地址:Linked List Cycle II - LeetCode Given a linked list, return the node where the cycle begins. If ...
- 【LeetCode】142 - Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- Leetcode 142. Linked List Cycle IIJAVA语言
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- 142. Linked List Cycle II 环形链表 II
给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 - ...
最新文章
- springboot 跨域解决方案
- Facebook成立AI语言研究联盟,自然语言处理或有新成果
- Spring Cloud (3) 服务消费者-Ribbon
- VC实现HTTP协议的GET和POST方法
- java图像处理之查找表实现图像处理加速
- linux shell if -a 到-z参数含义
- JavaScript学习(八十八)—数组知识点总结,超详细!!!
- python 错误代码_[python]WindowsError的错误代码详解
- 一个简单小说阅读网页html,简单版小说搜索阅读(64位程序)
- cf----2019-10-06(Slime,Shashlik Cooking,Mysterious Crime)
- python短信验证码_python发送短信验证码
- 艺术照片特效软件JixiPix Premium Pack Mac2020-10-15
- Turtle 画方形 画圆 画三角形
- 用于excel(或wps)中进行ip处理转换的vbs模块
- Manifest.json文档说明
- 《MySQL》入门基础知识点大全:数据库操作、增删改查、联表查询、常用函数、MD5加密、事务特性、隔离级别
- (区块链溯源)基于Hyperledger Fabric 的农产品区块链溯源方法
- 音乐播放器 —— C++
- 非侵入式负荷分解之BLUED数据集
- 简书markdown
热门文章
- matlab程序 surf算法,【求大神帮忙,surf算法源代码解析】
- 调参1——随机森林贝叶斯调参
- 解决Flink案例DataStream中使用keyBy(0),keyBy弃用的问题
- Tcpdump抓包实操
- 【题解】BZOJ5093图的价值(二项式+NTT)
- 启动/关闭Spring boot服务脚本
- Codeforces Round #417 (Div. 2)
- Chocolatey 简介(软件自动化管理工具)
- jquery学习之1.12-给节点添加样式
- Conditional