题目来源:

  https://leetcode.com/problems/linked-list-cycle-ii/


题意分析:

  给定一个链表,如果链表有环,返回环的起始位置,否则返回NULL。要求常量空间复杂度。


题目思路:

  首先可以用快慢指针链表是否有环。假设链表头部到环起点的距离为n,环的长度为m,快指针每次走两步,慢指针每次走一步,快慢指针在走了慢指针走t步后相遇,那么相遇的位置是(t - n) % m + n=(2*t - n)%m + n,那么得到t%m = 0,所以头部和相遇的位置一起走n步会重新相遇。那么,头部和相遇点再次走,知道相遇得到的点就为起点。


代码(python):

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None or head.next == None:
return None
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
tmp = head
while tmp != fast:
tmp,fast = tmp.next,fast.next
return tmp
return None

[LeetCode]题解(python):142-Linked List Cycle II的更多相关文章

  1. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  2. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

  3. 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)

    题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...

  4. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  5. Java for LeetCode 142 Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  6. 【LeetCode】142. Linked List Cycle II (2 solutions)

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  7. [LeetCode] 142. Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...

  8. [LeetCode] 142. Linked List Cycle II 链表中的环 II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  9. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  10. (链表 双指针) leetcode 142. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...

随机推荐

  1. json(转)

    转自:http://www.cnblogs.com/mcgrady/archive/2013/06/08/3127781.html 阅读目录 JSON的两种结构 认识JSON字符串 在JS中如何使用J ...

  2. 轻量级的数据交换语言(JSON)

    游戏开发过程中,很多用到JSON的地方:客户端与服务端的网络通信,程序读取客户端的数值表之类的. JSON用于描述数据结构,有以下形式存在. 对象(object):一个对象以“{”开始,并以“}”结束 ...

  3. Python进阶之函数式编程(把函数作为参数)

    什么是函数式编程? 什么是函数式编程? 函数:function 函数式:functional,一种编程范式 函数式编程是一种抽象计算的编程模式 函数≠函数式,比如:计算≠计算机 在计算机当中,计算机硬 ...

  4. GO语言搭建

    最近对GO语言产生了浓厚的兴趣.因为GO语言不仅仅可以开发桌面.web程序,最吸引我的是安卓大有往GO语言全方位靠拢的趋势,自家的系统还是用自家的语言开发比较靠谱. 用一句话来说:Go语言是谷歌200 ...

  5. [原创]linux简单之美(三)

    原文链接:linux简单之美(三) 在linux简单之美(二)中我们尝试使用了C库的函数完成功能,那么能不能用syscall方式来搞呢?显然可以! section .data ft db sectio ...

  6. Kill 正在执行的存储过程

    1.找到正在执行的存储过程的 sid ,serial# select   b.sid,b.SERIAL#,a.OBJECT, 'alter system kill session   ' || ''' ...

  7. C# 常用函数和方法集

    1.DateTime 数字型  System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System. ...

  8. 使用gSoap做一个简单的CS系统

    首先介绍一下gSoap,这是一个非常厉害的网络库,封装程度非常高而且跨平台. 支持SSL链接,数据格式为XML. 介绍地址: http://sourceforge.net/projects/gsoap ...

  9. 《windows程序设计》学习_4:文本输出,加滚动条

    //总行数 #define NUMLINES ((int) (sizeof sysmetrics / sizeof sysmetrics [0])) struct { int Index ; TCHA ...

  10. AngularJs (二) 搭建Deployd 服务爬坑

    Deployd 爬坑 按照书上的教程,介绍Deployd 这个东东,首先进入其deployd.com/网页,发现这个东东着实厉害. THE SIMPLEST WAY TO BUILD AN API 按 ...