leetcode 【 Linked List Cycle 】 python 实现
题目:
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
代码:oj在线测试通过 Runtime: 416 ms
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @return a boolean
def hasCycle(self, head):
if head is None or head.next is None:
return False p1 = ListNode(0)
p1.next = head
p2 = ListNode(0)
p2.next = head result = False
while p1 is not None and p2 is not None:
if p1 == p2:
result = True
break
else:
p1 = p1.next
p2 = p2.next
if p2 is not None:
p2 = p2.next return result
思路:
这是一道经典的题 关键点是快慢指针
p1是慢指针,一次走一步;p2是快指针,一次走两步;如果有循环,则快慢指针一定会在某一时刻遇上。
有个问题比较关键:为啥进入循环后,快指针一定能在某一时刻跟慢指针踩在同一个点儿上呢?
小白觉得可以如下解释:
假设现在快慢指针都在循环当中了,由于循环是个圈,则可以做如下的等价:“慢指针一次走一步,快指针一次走两步” 等价于 “慢指针原地不动,快指针一次走一步”这个其实跟物理学中的相对运动原理差不多。
欢迎高手来拍砖指导。
leetcode 【 Linked List Cycle 】 python 实现的更多相关文章
- leetcode Linked List Cycle python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [LeetCode] 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 单链表中的环
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- [算法][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 ...
- LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- LeetCode: Linked List Cycle II 解题报告
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- LeetCode: Linked List Cycle 解题报告
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- LeetCode Linked List Cycle 解答程序
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
随机推荐
- window下编译ffmpeg
网上关于编译ffmpeg的帖子很多,我也尝试了很多次,但是很多都过不了,一部分原因是版本问题,还有就是有的路劲没说的太明白导致的,经过一天的摸索,最终编译好了,下面把编译方式写下来,希望对看到帖子的人 ...
- UVA-674 Coin Change---完全背包
题目链接: https://vjudge.net/problem/UVA-674 题目大意: 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 思路: 每 ...
- 类型构造器--高阶类型(构造器):Kind (type theory)--类型的元
元类型(0阶类型):nullary type, data types 一元类型(一阶类型):unary adj. [数] 一元的 二元类型: is the kind of a binary ty ...
- 成绩累加排名,poj(2153)
题目链接:http://poj.org/problem?id=2153 解题报告: 注意map中的string,因此要将char[]转换为string型. #include <iostream& ...
- LayoutParams布局
AbsoluteLayout.LayoutParams可以重新设置坐标,然后调用setLayoutParamsLinearLayout.LayoutParams可以调用setMargins();来移动 ...
- R 语言学习日志 1
1. CSV文件的的读取与写出 2. 数据集筛选 3. 简单随机抽样 sample函数 正文: 1. CSV文件的的读取与写出 文件读取: df2 <- read.table(" ...
- 第16章 STM32中断应用概览—零死角玩转STM32-F429系列
第16章 STM32中断应用概览 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fi ...
- 如何把设计图自动转换为iOS代码? 在线等,挺急的!
这是一篇可能略显枯燥的技术深度讨论与实践文章.如何把设计图自动转换为对应的iOS代码?作为一个 iOS开发爱好者,这是我很感兴趣的一个话题.最近也确实有了些许灵感,也确实取得了一点小成果,和大家分享一 ...
- 利用bootstrap实现图片Carousel效果
引入头文件: <link rel="stylesheet" href="bootstrap.min.css"> <link rel=" ...
- JS - 给String.prototype添加replaceAll方法
String.prototype.replaceAll = function (targetStr, newStr) { var sourceStr = this.valueOf(); while ...