"""
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
Note: Do not modify the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.
"""
"""
判断链表是否有环。有两种做法
第一种是用set()存已经遍历过的结点
如果新的结点在set()里,则返回,有环
""" class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
def detectCycle(self, head):
nodes = set()
while head:
if head in nodes:
return head
nodes.add(head)
head = head.next
return None """
解法二:快慢指针
A→B→C 快指针:A, C, B, D 一次走两步
↖↓ 慢指针:A, B, C, D
D
根据距离推算:相遇点距环入口的距离 = (头节点距环入口的距离)*(快指针步数-1)
快慢指针相遇在D,让快指针变为head
同步向后,再次相遇即为环的起点
""" class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
def detectCycle(self, head):
if head == None or head.next == None:
return None
slow, fast = head, head
while fast:
slow = slow.next
fast = fast.next
if fast:
fast = fast.next
if fast == slow:
break
if slow != fast:
return None
fast = head #!!!关键所在
while slow:
if fast == slow:
return fast
fast = fast.next
slow = slow.next

leetcode142 Linked List Cycle II的更多相关文章

  1. LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

    链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

  2. Leetcode142. Linked List Cycle II环形链表2

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶: 你是否可以不用额外空间解决此题? 方法一:使用map 方法二: 分两个步骤,首先通 ...

  3. LeetCode 142. 环形链表 II(Linked List Cycle II)

    142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...

  4. LeetCode142:Linked List Cycle II

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

  5. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  6. 15. Linked List Cycle && Linked List Cycle II

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...

  7. 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 ...

  8. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  9. 【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 ...

随机推荐

  1. Nexus-vPC和STP BPDU

    1.为了交互vPC拓扑,STP机制被修改适应到vPC peer环境.2.对于vPC ports,只有主角色运行STP,换句话说,vPC下的STP由主角色设备控制.3.只有主角色设备在DP(指定端口)上 ...

  2. JavaScript动画相关

    JS动画原理 通过CSS缓慢变化从而实现动画效果 获取css属性 Window.getComputedStyle()方法返回一个对象,该对象在应用活动样式表并解析这些值可能包含的任何基本计算后报告元素 ...

  3. 【 hibernate 】基本配置

    hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibe ...

  4. TP-LINK路由器端口映射全套教程(亲测有效)

    最近想在自己的笔记本上搭建一个博客系统,方便自己写写日志,记录一些知识心得. 由于笔记本是长期放在家里的,需要在外边也能访问它,于是需要在路由器上设置一个端口映射,让在因特网上的其他电脑能访问到家里的 ...

  5. 五年C语言程序员,是深耕技术还是走管理?

    从进入程序员行列开始(2013年6月),到现在为止(2019年2月),已经有五年半了.    一路波折,已经从无知菜鸟走到了意识觉醒的老鸟了.    薪资变化情况如下: 2013年:2000元/月 ( ...

  6. 计算机基础- 序列化(Serialization)和持久化(Persistence)的区别

    参考 https://en.wikipedia.org/wiki/Serialization https://en.wikipedia.org/wiki/Persistence_(computer_s ...

  7. Swift3.0-基本运算符

    一.简介 运算符是检查.改变.合并值的特殊符号或者短语.在本篇文章中只介绍基本运算符,Swift中包含的高级运算符(比如溢出运算符)不在其中.Swift中的运算符和OC中的运算法还是有比较大的区别的, ...

  8. 安装luarocks安装驱动

    yum install libtermcap-devel ncurses-devel libevent-devel readline-devel--安装lua前提条件 LuaSQL 可以使用 LuaR ...

  9. Daemon——守护进程

    守护进程,也就是通常说的Daemon进程,是Linux中的后台服务进程.它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程常常在系统引导装入时启动, ...

  10. 7(计算机网络) ICMP与ping

    无论是在宿舍,还是在办公室,或者运维一个数据中心,我们常常会遇到网络不通的问题.那台机器明明就在那里,你甚至都可以通过机器的终端连上去看.它看着好好的,可是就是连不上去,究竟是哪里出了问题呢? ICM ...