Problem link:

http://oj.leetcode.com/problems/linked-list-cycle-ii/

The solution has two step:

Detecting the loop using faster/slower pointers.

Finding the begining of the loop. After two pointers meet in step 1, keep one pointer and set anther to the head. Let the two pointers both go one step for each iteration. The iteration terminates when two pointers meet, then the position of the two pointers is the begining node of the loop.

The correctness proof could be found in my homepage doc.

The python code for this problem is as follows.

  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6.  
  7. class Solution:
  8. # @param head, a ListNode
  9. # @return a list node
  10. def detectCycle(self, head):
  11. # Detect the loop
  12. p1 = head
  13. p2 = head
  14. while p2 is not None:
  15. p1 = p1.next
  16. if p2.next is None: # No loop
  17. return None
  18. p2 = p2.next.next
  19. if p1 == p2:
  20. break # have a loop
  21. if p2 is None:
  22. return None
  23.  
  24. # Find the start of the loop
  25. p1 = head
  26. while p1 != p2:
  27. p1 = p1.next
  28. p2 = p2.next
  29. return p1

【LeetCode OJ】Linked List Cycle II的更多相关文章

  1. 【LeetCode练习题】Linked List Cycle II

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  2. 【LeetCode OJ】Linked List Cycle

    Problem link: http://oj.leetcode.com/problems/linked-list-cycle/ We set two pointers: the faster poi ...

  3. 【LeetCode OJ】Pascal's Triangle II

    Problem Link: http://oj.leetcode.com/problems/pascals-triangle-ii/ Let T[i][j] be the j-th element o ...

  4. LeetCode OJ 142. Linked List Cycle II

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

  5. LeetCode OJ:Linked List Cycle II(循环链表II)

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

  6. 【Leetcode】Linked List Cycle II

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

  7. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

  8. 【题解】【链表】【Leetcode】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】【Medium】Linked List Cycle II

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

随机推荐

  1. Unity Invoke 方法

    Invoke() 方法是 Unity3D 的一种委托机制 如: Invoke("a", 5);   它的意思是:5 秒之后调用 a() 方法: 使用 Invoke() 方法需要注意 ...

  2. arguments的基本用法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. JavaWeb基础: 第一个Web应用(Servlet)

    Servlet的生命周期 <servlet-mapping>和<servlet> Web应用的用户是通过指定浏览器中URL地址来访问Web应用提供的静态或者是动态资源,如果Se ...

  4. 用Visual C#向access添加数据

    (1)创建并打开一个OleDbConnection对象. (2)创建一个插入一条记录的SQL语句. (3)创建一个OleDbCommand对象. (4)通过此OleDbCommand对象完成对插入一条 ...

  5. visual studio 插件开发

    插件的定义 所谓插件,就是根据平台接口开发的第三方程序.第一次听到这个名词很是不了解,听了解释也不是很明白,那我们来举个例子,比如说一辆房车,现在里面只有基本的一些设施,但是你现在想在顶部有一个晒太阳 ...

  6. linux 查看僵尸进程

    top -b -i -n 1 查看僵死进程命令 ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' 查看apache 当前进程数 ps -ef | grep ht ...

  7. myeclipse 6.5配置tomcat7.X

    软件安装:myeclipse 6.5 Apache Tomcat/7.0.47 安装完成后开始配置 1. 随便展开一个tomcat配置选项,这里配置到tomcat6.x,如下图:

  8. sphinx全文检索功能 | windows下测试 (二)

    sphinx 数据库检索需要对数据库重新生成索引,为自己所用,然后按照拆词匹配

  9. jquery之getJSON方法获取中文数据乱码解决方法

    最近公司做的东西要用到js,感觉js太繁琐,所以自己学起了jquery,发现jquery确实强大.在学到jquery ajax的时候(用的工具是eclipse),发现$.getJSON()方法请求服务 ...

  10. NSDate的运算

    NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...