"""
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. Codeforces Round #611 (Div. 3) C

    There are nn friends who want to give gifts for the New Year to each other. Each friend should give ...

  2. BOSS标准版-电话收费结账明细费用sql语句

    明细费用页面-所在jsp路径-:/EtcomWeb_BZvx/WebRoot/hfys/manage/telCharge/detailInfo.jsp exec Hfys_Sp_CaculCharge ...

  3. Mysql 8.0 新特性测试

    Mysql 8.0 新特性测试 Role MySQL8.0版本添加了role特性,role是一种逻辑概念是权限的集合,可以将一个或以上的权限赋予给role,再将role赋给user.Oracle,Po ...

  4. locust --hellp

    1. Locust简介 Locust是使用Python语言编写实现的开源性能测试工具,简洁.轻量.高效,并发机制基于gevent协程,可以实现单机模拟生成较高的并发压力. 官网:https://loc ...

  5. 【STM32H7教程】第54章 STM32H7的LTDC应用之LCD电阻触摸和电容触摸

    完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第54章       STM32H7的LTDC应用之LCD电阻 ...

  6. 50道SQL练习题及答案与详细分析(MySQL)

    50道SQL练习题及答案与详细分析(MySQL) 网上的经典50到SQL题,经过一阵子的半抄带做,基于个人理解使用MySQL重新完成一遍,感觉个人比较喜欢用join,联合查询较少 希望与大家一起学习研 ...

  7. 1123. Lowest Common Ancestor of Deepest Leaves

    link to problem Description: Given a rooted binary tree, return the lowest common ancestor of its de ...

  8. [转载] 由浅入深聊聊Golang的map

    https://blog.csdn.net/u011957758/article/details/82846609 几个我忽略的点 基础 map中value的默认值 key与value的限制 valu ...

  9. char、pchar、string互相转换

    1.string转换成pchar 可以使用pchar进行强制类型转换,也可以使用StrPCopy函数 var s:string; p,p1:PChar; begin s:='Hello Delphi' ...

  10. linux搭建jenkins+github详细步骤

    事情缘由: 现在在做的主要工作是通过jenkins+postman实现api的自动化测试,想要达到的效果是,api自动化测试定时跑脚本的同时,github有新的代码提交,jenkins会自动检测部署新 ...