【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. 解题: ...
随机推荐
- c# BackGroundWorker 多线程操作的小例子
在我们的程序中,经常会有一些耗时较长的运算,为了保证用户体验,不引起界面不响应,我们一般会采用多线程操作,让耗时操作在后台完成,完成后再进行处理或给出提示,在运行中,也会时时去刷新界面上的进度条等显示 ...
- hdu---(2604)Queuing(矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- struts2 <s:iterator> status属性
struts2 <s:iterator> status属性 转载▼ iterator标签主要是用于迭代输出集合元素,如list set map 数组等,在使用标签的时候有三个属性值得我 ...
- Python eclipse开发环境搭建
http://jingyan.baidu.com/article/cd4c2979101f02756f6e6064.html http://jingyan.baidu.com/article/1876 ...
- 数据库索引<二> 补充前篇
你要准备的软件有: 最新版 Rsync for windows 服务端:cwRsync_Server_2.1.5_Installer.zip 客户端:cwRsync_2.1.5_Installer.z ...
- AJAX是什么? AJAX的交互模型(流程)?同步和异步的区别? AJAX跨域的解决办法?
AJAX是什么? AJAX的交互模型(流程)?同步和异步的区别? AJAX跨域的解决办法? 分类: web前端面试题2013-07-20 22:40 630人阅读 评论(0) 收藏 举报 目录(? ...
- mysql启动错误
1.启动时,显示ERROR tail localhost.localdomain.err 错误日志 2.新增目录,启动成功
- php验证用户名是否以字母开头与验证密码
验证用户名是否以字母开头与验证密码只能为数字和字母的组合代码三款三种常用验证函数 验证邮箱地址格式 验证密码只能为数字和字母的组合 验证用户名是否以字母开头代码哦,这是用户注册时或提交表单时会用的哦 ...
- Facebook技术架构
Facebook MySQL,Multifeed (a custom distributed system which takes the tens of thousands of updates f ...
- 复利计算4.0单元测试(c语言)
——————————复利计算程序单元测试报告—————————— ————————————4.0 单元测试—————————————— ————————————————要求—————————————— ...