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 ...
随机推荐
- IKAnalyzer使用
1.分析器 所有分析器最终继承的类都是Analyzer 1.1 默认标准分析器:StandardAnalyzer 在我们创建索引的时候,我们使用到了IndexWriterConfig对象,在我们创建索 ...
- Java IO流详解(二)——File类
在上一章博客中简单的介绍了Java IO流的一些特征.也就是对文件的输入输出,既然至始至终都离不开文件,所以Java IO流的使用得从File这个类讲起. File类的描述:File类是文件和目录路径 ...
- 【PAT甲级】1042 Shuffling Machine (20 分)
题意: 输入洗牌次数K(<=20),输入54张牌每次洗入的位置(不是交换的位置),输出洗好的牌. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...
- FFplay 命令
1. 查看支持的格式: ffplay.exe -formats 2. 播放PCM裸流: ffplay.exe - -channels -f s16le -i pcm_file_path 根据PCM文件 ...
- day03-MyBatis的动态SQL语句查询
场景一: 例如当我们想实现这样的查询的时候之前的操作貌似满足不了我们. 场景二: 还有些时候我们如果输入的信息越多满足要求的就越多,所查找出来的用户就越少 当我们之输入姓名的时候可能查找出10个人, ...
- linux磁盘空间挂载
(1)查看磁盘空间 df -hl (3)查看硬盘及分区信息 fdisk -l (4)格式化新分区 mkfs.ext3 /dev/xvdb (5)将磁盘挂载在/www/wwwroot/default目录 ...
- idea代码神器:根据表生成代码
Easycode是idea的一个插件,可以直接对数据的表生成entity,controller,service,dao,mapper,无需任何编码,简单而强大. 1.安装(EasyCode) 我这里的 ...
- 10 从DFS到DFT
从DFS到DFT 周期序列的级数展开 正如连续时间周期信号可以表示为一系列正弦信号的和的形式,周期序列也可以表示为一系列正弦之和的形式,假设序列\(\tilde{x}[n]\)的周期为\(N\),那 ...
- ES5-严格模式
在es5中可以开启一种严格模式的代码形式,开启方式是:将全局或者函数的第一条语句定义为:'use strict';. 如果浏览器不支持,会将其解析为一条普通语句,没有任何的副作用. 开启全局模式后会有 ...
- WPS 2019文档编辑 技巧
wps2019文档怎么设置粘贴时自动匹配当前的格式: 菜单栏 文件 -- 选项 -- 编辑 -- 默认粘贴方式 设置为 匹配当前格式. 在编辑文档左侧或右侧显示/隐藏 目录/书签: 视图 -- 导航窗 ...