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. SqlServer关闭与启用标识(自增长)列

    1 --添加新列 2 ALTER TABLE TABLENAME ADD ID int 3 --赋值 4 UPDATE TABLENAME SET ID = IDENTITY_ID 5 --删除标识列 ...

  2. fork&exec

    进程是系统进行资源分配和调度的基本单位,包括代码.数据和PCB进程控制块等资源. fork函数通过系统调用创建一个与原进程相同的子进程. 在调用进程(父进程)中返回一次,返回子进程ID:在子进程返回0 ...

  3. 《JavaScript权威指南》读书笔记(一)

    日期 2015-11-28 把之前的读书笔记在我弄丢它之前搬过来~~ 时间过去好久,回头一看理解都不一样了. 重点浏览了一下和Java的不同之处: js是一种宽松类型语言:js不区别整形数值与浮点型数 ...

  4. JDK1.7-LinkedList循环链表优化

    最近在看jdk1.7的时候,发现LinkedList 和1.6中的变化. 首先,简单介绍一下LinkedList: LinkedList是List接口的双向链表实现.由于是链表结构,所以长度没有限制: ...

  5. DOM解析和SAX解析的区别

    DOM解析和SAX解析的区别 博客分类: XML DOM SAX  DOM解析和SAX解析的区别 No 区 别 DOM解析 SAX解析 1 操作 将所有文件读取到内存中形成DOM树,如果文件量过大,则 ...

  6. [工作需求]linux常用命令以及vim常用命令

    一.             Linux 常用命令 mkdir dirname新建文件夹 cd ~ 进入自己的家目录 cd dirname 进入名字为dirname的目录: l 显示当前文件夹下的文件 ...

  7. 安卓/res/menu/的使用

    <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http:/ ...

  8. sql删除语句

    TRUNCATE TABLE Moisture_test 删除表里所有的数据,就连主键的自增也被删除delete Moisture_test 删除表里数据但是就连主键的自增没有被删除

  9. LinearLayout练习

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  10. 面试题目-findmax的实现

    #include <vector> #include <iostream> #include "printCollection.h" using names ...