【LeetCode OJ】Linked List Cycle II
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的更多相关文章
- 【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 ...
- 【LeetCode OJ】Linked List Cycle
Problem link: http://oj.leetcode.com/problems/linked-list-cycle/ We set two pointers: the faster poi ...
- 【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 ...
- 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 ...
- 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 ...
- 【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 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 ...
- 【题解】【链表】【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【Leetcode】【Medium】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 解题: ...
随机推荐
- Unity Invoke 方法
Invoke() 方法是 Unity3D 的一种委托机制 如: Invoke("a", 5); 它的意思是:5 秒之后调用 a() 方法: 使用 Invoke() 方法需要注意 ...
- arguments的基本用法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JavaWeb基础: 第一个Web应用(Servlet)
Servlet的生命周期 <servlet-mapping>和<servlet> Web应用的用户是通过指定浏览器中URL地址来访问Web应用提供的静态或者是动态资源,如果Se ...
- 用Visual C#向access添加数据
(1)创建并打开一个OleDbConnection对象. (2)创建一个插入一条记录的SQL语句. (3)创建一个OleDbCommand对象. (4)通过此OleDbCommand对象完成对插入一条 ...
- visual studio 插件开发
插件的定义 所谓插件,就是根据平台接口开发的第三方程序.第一次听到这个名词很是不了解,听了解释也不是很明白,那我们来举个例子,比如说一辆房车,现在里面只有基本的一些设施,但是你现在想在顶部有一个晒太阳 ...
- linux 查看僵尸进程
top -b -i -n 1 查看僵死进程命令 ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' 查看apache 当前进程数 ps -ef | grep ht ...
- myeclipse 6.5配置tomcat7.X
软件安装:myeclipse 6.5 Apache Tomcat/7.0.47 安装完成后开始配置 1. 随便展开一个tomcat配置选项,这里配置到tomcat6.x,如下图:
- sphinx全文检索功能 | windows下测试 (二)
sphinx 数据库检索需要对数据库重新生成索引,为自己所用,然后按照拆词匹配
- jquery之getJSON方法获取中文数据乱码解决方法
最近公司做的东西要用到js,感觉js太繁琐,所以自己学起了jquery,发现jquery确实强大.在学到jquery ajax的时候(用的工具是eclipse),发现$.getJSON()方法请求服务 ...
- NSDate的运算
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...