[LeetCode]题解(python):142-Linked List Cycle II
题目来源:
https://leetcode.com/problems/linked-list-cycle-ii/
题意分析:
给定一个链表,如果链表有环,返回环的起始位置,否则返回NULL。要求常量空间复杂度。
题目思路:
首先可以用快慢指针链表是否有环。假设链表头部到环起点的距离为n,环的长度为m,快指针每次走两步,慢指针每次走一步,快慢指针在走了慢指针走t步后相遇,那么相遇的位置是(t - n) % m + n=(2*t - n)%m + n,那么得到t%m = 0,所以头部和相遇的位置一起走n步会重新相遇。那么,头部和相遇点再次走,知道相遇得到的点就为起点。
代码(python):
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None or head.next == None:
return None
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
tmp = head
while tmp != fast:
tmp,fast = tmp.next,fast.next
return tmp
return None
[LeetCode]题解(python):142-Linked List Cycle II的更多相关文章
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- 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】142. Linked List Cycle II (2 solutions)
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- [LeetCode] 142. Linked List Cycle II 链表中的环 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【LeetCode】142. Linked List Cycle II
Difficulty:medium More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...
- (链表 双指针) leetcode 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
随机推荐
- java:添加一条数据到数据库中文乱码
在数据库链接地址后面加上:characterEncoding=UTF8 如:jdbc\:mysql\://localhost\:3306/db_sjzdaj?relaxAutoCommit=true& ...
- MySQL报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password:NO)
1.关闭mysql # service mysqld stop2.屏蔽权限 # mysqld_safe --skip-grant-table 屏幕出现: Starting demo fro ...
- Invalid signature file digest for Manifest main attributes
Solving a Spark error: Invalid signature file digest for Manifest main attributes When using spark-s ...
- nodeJs入门笔记(二)
js中window通常是全局变量 global 是node.js里的全局变量 node中能访问的对象一般都是 global的 属性 global 对象属性 process 用于描述当前Node 进程状 ...
- java面向对象之 封装 Encapsulation
什么是封装:对象中的成员该隐藏的隐藏.该公开的要公开 封装:顾名思义,隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别:将抽象得到的数据和行为(或功能)相结合,形成一个有 ...
- 使用Notepad++开发python配置笔记
这是我在python学习过程中,收集整理的一些notepadd++环境配置方法. 1.配置制表符 Notepad++ ->"设置"菜单->"首选项" ...
- EasyUI在MVC4中需要部分刷新页面时load()后页面变形问题!
最近在使用MVC4与EasUI过程中遇到些容易导致界面变形的问题,纠结了很久,但其实当发现问题在哪里时,倒觉得最终还是自己对MVC4的概念没把握好,OK,show time. 本示例Contact ...
- windbg命令学习2
一.windbg查看内存命令: 当我们在调试器中分析问题时, 经常需要查看不同内存块的内容以分析产生的原因, 并且在随后验证所做出的假设是否正确. 由于各个对象的状态都是保存在内存中的, 因此内存的内 ...
- FAQ:Python中*args和**agrs的区别
python提供了两种特别的方法来定义函数的参数: 1. 位置参数 *args, 把参数收集到一个元组中,作为变量args >>>def show_args(*args): ...
- 解密电子书之二:EPD控制芯片
EPD控制芯片大致上相当于计算机的显卡,没了它,所有电子书都变白板.类似显卡中的ATI与NVIDIA,EPD控制芯片中也是两家:Surf(泰信科)和EPSON(爱普生),其中爱普生是最早推出电子纸显示 ...