1  题目:

对于一个给定的链表,返回环的入口节点,如果没有环,返回null

拓展:

你能给出不利用额外空间的解法么?

代码:

class Solution {
public:ListNode *detectCycle(ListNode *head) {}
};

2  解法

2.1  解法1(答案最高亮):

2.1.1  思路:

1)同linked-list-cycle-i一题,使用快慢指针方法,判定是否存在环,并记录两指针相遇位置(Z);

2)将两指针分别放在链表头(X)和相遇位置(Z),并改为相同速度推进,则两指针在环开始位置相遇(Y)。

2.1.2  证明:

如下图所示,X,Y,Z分别为链表起始位置,环开始位置和两指针相遇位置,则根据快指针速度为慢指针速度的两倍,可以得出:

2*(a + b) = a + b + n * (b + c);即

a=(n - 1) * b + n * c = (n - 1)(b + c) +c;

注意到b+c恰好为环的长度,故可以推出,如将此时两指针分别放在起始位置和相遇位置,并以相同速度前进,当一个指针走完距离a时,另一个指针恰好走出 绕环n-1圈加上c的距离。

故两指针会在环开始位置相遇。

2.1.3  代码:

class Solution {
public:ListNode *detectCycle(ListNode *head) {if(head == NULL){return 0;}ListNode* slow = head;ListNode* fast = head;while(fast != NULL && fast->next != NULL){slow = slow->next;fast = fast->next->next;if(slow == fast){break;}}if(fast == NULL || fast->next == NULL){return NULL;}slow = head;while(slow != fast){slow = slow->next;fast = fast->next;}return slow;}
};

2.1.4  疑问以及思考

上述逻辑的前提是慢针到达Y后只走了b步就被快针追上了,有没有可能慢针到达Y后又多转了几圈才被快针追上呢?

答案是不会的。

还是上面那张图,但是含义不一样的,假如慢针到达Y之后快针在Z(只是单纯想用上面的图,此时的Z不代表相遇地点,而是代表慢针到达Y之后快针的位置),那么此时快针落后慢针c步被追上,那么也就是说慢针还要走c步会被快针追上。而由图可知,c是小于环的长度的,所以不存在慢针到达Y后走了多于一圈才被快针追上的可能。

leetcode 142 --- linked-list-cycle-ii相关推荐

  1. Leetcode 142. Linked List Cycle II

    地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...

  2. 【To Do】LeetCode 142. Linked List Cycle II

    LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...

  3. 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 ...

  4. [LeetCode] 142. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 题意:找 ...

  5. Leetcode - 142. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 分析: ...

  6. 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 ...

  7. 【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 ...

  8. 142. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  9. 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 ...

  10. 142. Linked List Cycle II 环形链表 II

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 - ...

最新文章

  1. 美国人的网站推广方式千奇百怪
  2. 关键词提取_NLP 关键词 提取 实战 案例
  3. c语言产生随机数_C语言 求的近似值
  4. 诗歌rails 之with_options
  5. LeetCode Algorithm 35. 搜索插入位置
  6. Spring Session实战3
  7. string 字符串中字符无效_JavaScript中的字符串(string)到底是什么?
  8. 小 Q 与树(dsu on tree + segment tree)牛客练习赛 81 D
  9. Flex布局 让你的布局更完美
  10. 多stream_基础之Lambda和Stream的邂逅
  11. 探秘 Dubbo 的度量统计基础设施 - Dubbo Metrics
  12. socket timeout是什么引起的_MySQL C API 参数 MYSQL_OPT_READ_TIMEOUT 的一些行为分析
  13. 迭代器、生成器、递归、二分法
  14. 贵州大学旧物交易系统
  15. android textwatcher 延时,Android TextWatcher监控EditText中的输入内容并限制其个数
  16. python excel数据合并_如何用python将excel数据合并
  17. 使用 styled-components 定义组件样式
  18. linksys 打印软件_Linksys固件DD-WRT BitTorrent优化
  19. 人脸检测库libfacedetection使用方法
  20. OpenCore 版本升级后清除NVRAM

热门文章

  1. c语言xc比较大小写,XCTestAPI文档.docx
  2. 生产环境可以用吗_柑橘可以生产果醋吗?柑橘果醋的生产工艺是什么?
  3. 数字滤波器的差分方程和传递函数
  4. ubuntu安装谷歌浏览器 typora+出现编码错误‘ascii‘ codec can‘t encode character ‘\u6b66‘+docker里安装tensorrt报错
  5. p1和p7签名的区别
  6. app store 服务器维护,AppStore无法连接怎么办?几个小方法教你解决问题
  7. promise的状态值_什么是Promise,Promise的三种状态
  8. python登录豆瓣_python登录豆瓣,发帖
  9. 哪个html标签用于定义文档的标题,在HTML中,(41)用于定义文档的标题。
  10. mybatis调用oracle过程,使用MyBatis调用Oracle存储过程