"""
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. component:(resolve) => require

    resolve => require(['../pages/home.vue'], resolve)这种写法是异步模块获取,打包的时候每次访问这个路由的时候会单调单个文件,按需加载,不过这种写法 ...

  2. DNS域名解析服务(重点)

    一 .DNS  系统的作用 1.DNS 服务器概述 DNS 系统在网络中的作用就是维护着一个地址数据库,其中记录了各种主机域名:与 IP地址的对应关系,以便为客户程序提供正向或反向的地址查询服务,即正 ...

  3. selected中第一项 请选择,隐藏

    如何做到selected 类似input的提示语  placeholder效果. <select class="wyj_dbfs"> <option style= ...

  4. 实验一&#160;&#160;GIT 代码版本管理

    实验一  GIT 代码版本管理 实验目的: 1)了解分布式分布式版本控制系统的核心机理: 2)   熟练掌握git的基本指令和分支管理指令: 实验内容: 1)安装git 2)初始配置git ,git ...

  5. 缓存ViewState减少网络传输

    在维护 asp.net webform系统时,某些系统将控件及页面数据都存储在viewstate中,导致在频宽不够时,影响页面加载速度,此时可将viewstate 存储在服务端,减少网络传输. 重写  ...

  6. JavaScript - 编译性还是解释性?

    疑问 在JS的变量和声明式函数的提升看到了"预编译/预处理/预解释"中"预编译"这个字眼,产生了一个疑问:JS是熟知的解释性语言,但JS能被编译吗? 参考 ht ...

  7. DVWA靶机-sql自动注入

    1. 使用dvwa靶机进行sql注入实战(注:当前靶机安全级别为low) 打开sql漏洞,发现输入不同的数字会返回不同的信息, 先尝试手工判断是否存在sql注入 一般sql注入语句像这样,我们构造的是 ...

  8. idea设置自带的maven为国内镜像

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/panchang199266/articl ...

  9. 关于and 和or的执行优先级问题分析

    题目:列出本店价低于60或者高于100.并且商品点击数大于628的商品. 按照下面两种写法,得到的结果是不同的. 第一种:结果数据中有点击数为628的记录,显然不符合题目要求. SELECTgoods ...

  10. js运动框架及应用

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...