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.

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @return a list node
def detectCycle(self, head):
# Detect the loop
p1 = head
p2 = head
while p2 is not None:
p1 = p1.next
if p2.next is None: # No loop
return None
p2 = p2.next.next
if p1 == p2:
break # have a loop
if p2 is None:
return None # Find the start of the loop
p1 = head
while p1 != p2:
p1 = p1.next
p2 = p2.next
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. JavaScript自定义类和对象的方法

    备注:JavaScript中没有类class的概念,一般把原型对象看作类 1. 工厂方法--使用new Object创建对象并添加相关属性 var Obj = new Object;         ...

  2. mysql知识初篇(一)

    mysql介绍 (1) mysql数据库是瑞典AB开发. (2) mysql--> sun --> oracle. (3) mysql数据库的特点. 1. 开源. 2. 免费. 3. 跨平 ...

  3. sublime text2 解决中文乱码

    打开Preferences菜单,并选择 Browse Packages… 系统会打开Sublime Text 2的Packages文件夹,回到上一级菜单,然后打开Installed Packages文 ...

  4. linux shell 当前时间

    #!/bin/bashdatestr=`date --date='today' "+%Y-%m-%d %H:%M:%S"`echo $datestr

  5. Linux 远程桌面控制

    我现在知道有两种方式: 1.直接使用Gnome桌面的远程控制功能.在服务器端登录到gnome桌面,然后在系统菜单中打开远程桌面配置,勾选允许远程即可.这种方式客户端和服务器的两种操作将保持同步,也就是 ...

  6. qml package 的使用

    什么时候使用这个.就是多个view使用同一个deleagte的时候. The Package class is used in conjunction with VisualDataModel to ...

  7. heartbeat安装

    wget ftp://mirror.switch.ch/pool/1/mirror/scientificlinux/6rolling/i386/os/Packages/epel-release-6-5 ...

  8. 在 Linux 中怎样将 MySQL 迁移到 MariaDB 上

    自从甲骨文收购 MySQL 后,由于甲骨文对 MySQL 的开发和维护更多倾向于闭门的立场,很多 MySQL 的开发者和用户放弃了 MySQL.在社区驱动下,促使更多人移到 MySQL 的另一个叫 M ...

  9. SQL语句的用法

    1.增加字段     alter table docdsp     add dspcodechar(200)2.删除字段     ALTER TABLE table_NAME DROP COLUMNc ...

  10. 怎么学好python?

    文章摘自:http://www.jb51.net/article/16100.htm 1)学好python的第一步,就是马上到www.python.org网站上下载一个python版本.我建议初学者, ...