leetcode142 Linked List Cycle II
"""
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的更多相关文章
- 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 ...
- Leetcode142. Linked List Cycle II环形链表2
给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶: 你是否可以不用额外空间解决此题? 方法一:使用map 方法二: 分两个步骤,首先通 ...
- LeetCode 142. 环形链表 II(Linked List Cycle II)
142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...
- LeetCode142:Linked List Cycle II
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
- [算法][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 ...
- 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 ...
- 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 ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 【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 ...
随机推荐
- 杭电 1203 I NEED A OFFER!
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 【SSM sql.xml】日志查询mapper.xml
LogInfoMapper.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapp ...
- Tomcat热部署与热加载!
所谓的热部署与热加载就是两个值:(reloadable='true'与autoDeloy='true')
- 关于python 3.x import matplotlib as plt ImportError: DLL load failed: 找不到指定的模块
windows 10下使用conda update --all更新过后,就出现这样的问题了,各种包不能用了,然后在stackoverflow上搜到有人也遇到相同的问题,并通过其中的回答找到了原因,这里 ...
- locust --hellp
1. Locust简介 Locust是使用Python语言编写实现的开源性能测试工具,简洁.轻量.高效,并发机制基于gevent协程,可以实现单机模拟生成较高的并发压力. 官网:https://loc ...
- jemter-plugins-maven dependency -WIiki用法配置介绍
1.先介绍下jmeter 的maven中央仓库地址,有兴趣自己看下 https://mvnrepository.com/artifact/org.apache.jmeter 2.Wiki github ...
- 「JSOI2010」找零钱的洁癖
「JSOI2010」找零钱的洁癖 传送门 个人感觉很鬼的一道题... 首先我们观察到不同的数最多 \(50\) 个,于是考虑爆搜. 但是这样显然不太对啊,状态数太多了. 然后便出现了玄学操作: \(\ ...
- 面试官:说说Spring中的事务传播行为
前言 在开发中,相信大家都使用过Spring的事务管理功能.那么,你是否有了解过,Spring的事务传播行为呢? Spring中,有7种类型的事务传播行为.事务传播行为是Spring框架提供的一种事务 ...
- 利用正则表达式判断Java中的秒钟、分钟、小时、日、月是否符合规则
// 定义校验规则 Pattern patRule = Pattern.compile("判断规则"); // 校验结果 patRule.matcher("判断的对象&q ...
- Linux之用户和用户组总结
Linux是多用户.多任务操作系统 UID即为用户身份号码,具有唯一性,可通过UID来判断用户身份,有以下几种:UID为0,系统管理员,即root,万能:UID为1-999,系统账号,用于独立执行某些 ...