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

Follow up:
Can you solve it without using extra space?

题意:

给定一个链表,找到环起始的位置。如果环不存在,返回NULL。

分析:

(1)首先要判断该链表是否有环。如果没有环,那么返回NULL。

(2)其次,当已知环存在后,寻找环起始的位置。

思路:

(1)链表是否有环,利用快慢指针很好解决。

(2)当环存在时,寻找环的起始位置:

++++使用额外的空间,可以用一个map存放链表的节点,遍历链表:

如果当前节点在map中没有出现,那么将该节点加入到map中,否则,返回当前节点(也就是环的起点)。

-----不使用额外空间,利用快慢指针的特性。

  1. 1st ruond:
  2. SP(start point) MP(match point)
  3. A: --------------------------------------------------------MP
  4. B: - - - - - - - - - - - - - MP
  5.  
  6. 2nd round:
  7. B: MP- - - - - -- - - - - - - - -MP
  8. C:---------------------------------------------------------MP

  9. 3rd round:
  10. B1:- - - - - - - - - - - - - MP
  11. B2: MP- - - - - - - - - - - - - - MP
  12.  
  13. B1B2必定会在途中相遇。 那么,他们首次相遇的点即为环的起始点。

设定两个指针A,B都从SP(Start point)出发: A每次向后移动两个节点,B每次向后移动一个节点。

假设A,B在MP(match point)相遇。 那么A所走的路径长度为B所走的路径长度的2倍:La = 2*Lb。

假设有另一个指针C还是从SP启动,每次向后移动两个节点, B从MP开始出发每次向后移动一个节点,那么可以想象C和B会在MP相遇。

如果C从SP出发,每次只移动一个节点,B依旧从MP出发且每次向后移动一个节点, 那么B和C依旧会在MP相遇。

  1. class Solution {
  2. public:
  3. ListNode *detectCycle(ListNode *head) {
  4. // list length less than two
  5. if(head==NULL || head->next==NULL) return NULL;
  6. // if cycle exists in the list
  7. ListNode *startA= head->next;
  8. ListNode *endA= head->next->next;
  9. while(endA!=NULL && endA != startA){
  10. endA=endA->next;
  11. if(endA){
  12. endA=endA->next;
  13. startA=startA->next;
  14. }
  15. }
  16. // no cycle exists
  17. if(endA==NULL) return NULL;
  18. // find a cycle then search the start point
  19. ListNode *match = startA;
  20. ListNode *start = head;
  21. while(start!=match){
  22. start = start->next;
  23. match = match->next;
  24. }
  25. return start;
  26. }
  27. };
作者:Double_Win
由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~
 

[LeetCode 题解]: Linked List Cycle II的更多相关文章

  1. [Leetcode Week6]Linked List Cycle II

    Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...

  2. Java for 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 ...

  3. 【Leetcode】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 链表中的环 II

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

  5. 【题解】【链表】【Leetcode】Linked List Cycle II

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

  6. [LeetCode] 142. Linked List Cycle II 单链表中的环之二

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

  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. To r ...

  8. leetcode 142. Linked List Cycle II

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

  9. 【leetcode】Linked List Cycle II (middle)

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

随机推荐

  1. python命令行中import caffe报错的解决方案

    1.ImportError: No module named skimage.io >>> import caffe Traceback (most recent call last ...

  2. SmallLocks

    folly/SmallLocks.h This module is currently x64 only. This header defines two very small mutex types ...

  3. python的requests模块

    使用python进行接口测试得时候可以使用requests模块,是基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库 安装requests是模块 pip instal ...

  4. C# 二元一次方程参数求解

    本文记录了关于求直线斜率及纵截距值的简单方法,只是简单的记录下求解思路,最终还需根据具体项目进行优化. 设直线方程式为:y=kx+b 编程思想: 1.代入y1与x1的值,得到:y1=kx1+b 2.代 ...

  5. VS2017连接Oracle设置

    1. 下载安装 Oracle Developer Tools for Visual Studio 2017: 2. 配置tnsnames.ora ODP.Net默认使用安装目录下的 tnsnames. ...

  6. MyEclipse8.6启动后提示内存不足的解决方案(亲测,完美解决)

    转自:http://www.bubuko.com/infodetail-1625857.html 最近可能由于公司项目大了,启动MyEclipse后经常提示内存不足的警告框,如下: 其实点击close ...

  7. SC命令安装window服务

    sc create Styler binpath= "D:\Styler\Styler.exe" start= auto displayname= "Styler&quo ...

  8. javascript中defer的作用

    javascript中defer的作用 <script src="../CGI-bin/delscript.js" defer></script>中的def ...

  9. javascript中所谓的“坑”收录

    坑一: // 反例myname = "global"; // 全局变量function func() { alert(myname); // "undefined&quo ...

  10. Node.js究竟是什么?

    来源:https://www.ibm.com/developerworks/cn/opensource/os-nodejs/index.html?ca=drs#ibm-pcon   Node 旨在解决 ...