【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#引用Office.word出错的解决办法-无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass” 【转】
本文章转自 suchso 1.系统找不到 Microsoft.Office.Interop.Word" "Could not load file or assembly 'Micr ...
- Run “mvn clean install” in Eclipse
1.在Maven项目或者pom.xml上右键-->Run As -->"Maven Build..."或者"Run Configuration-->Ma ...
- IntelliJ IDEA 12 与 Tomcat 集成并运行Web项目
配置Tomcat Server 1.Ctrl+Alt+s或者File——>Setting...;选中“Application Servers”点击"+" 创建运行配置 上面的 ...
- python中关闭文件
1.关闭文件,通过f.write把内容写入文件会覆盖之前文件中的内容
- 9. shell环境
• printenv –打印部分或所有的环境变量 • set –设置 shell 选项 • export —导出环境变量,让随后执行的程序知道. • alias –创建命令别名 1.shell环境:s ...
- Hadoop 添加删除数据节点(datanode)
前提条件: 添加机器安装jdk等,最好把环境都搞成一样,示例可做相应改动 实现目的: 在hadoop集群中添加一个新增数据节点. 1. 创建目录和用户 mkdir -p /app/hadoop gr ...
- uva 1220
1220 - Party at Hali-Bula Time limit: 3.000 seconds Dear Contestant, I'm going to have a party at my ...
- Java--常用类summary
/* 2:API的概述(了解) (1)应用程序编程接口. (2)就是JDK提供给我们的一些提高编程效率的java类. 3:Object类(掌握) (1)Object是类层次结构的根类,所有的类都直接或 ...
- Maven 系列 二 :Maven 常用命令,手动创建第一个 Maven 项目【转】
1.根据 Maven 的约定,我们在D盘根目录手动创建如下目录及文件结构: 2.打开 pom.xml 文件,添加如下内容: <project xmlns="http://maven.a ...
- [转]C#设置WinForm快捷键
1.Alt+*(按钮快捷键)按钮快捷键也为最常用快捷键,其设置也故为简单.在大家给button.label.menuStrip等其他控件的Text属性指定名称时,在其后面加上‘&’然后在加上一 ...