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 ...
随机推荐
- SAP CRM WebClient UI和Hybris backoffice UI开发的相同点
CRM WebClient和Hybris backoffice的UI开发都不需要开发人员手写原生的html代码. CRM WebClient UI 在CRM WebUI workbench里,开发人员 ...
- IOS GCD03-其他用法
#define global_queue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) #define main_queu ...
- now()与sysdate()的区别(1)
now()与sysdate()两个函数都以'YYYY-MM-DD HH:MM:SS'的形式表示表示当前的时间.比如: root@rac1 21:13:10> select sysdate ...
- hdu-1247 Hat’s Words---字典树模板
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1247 题目大意: 给出一些单词,以EOF结束,看其中哪一个单词可以由其他两个单词组成,将其输出 解题 ...
- 【转】Android 组件系列-----Activity保存状态
本篇随笔将详细的讲解Activity保存状态的概念,也就是saving activity state. 一.Activity状态保持概念 保存Activity的状态是非常重要的,例如我们在玩一个游戏的 ...
- Visual Studio 2013 ReportViewer Control
最近需要给学生讲报表,.NET的自然就是选择RDLC了. 因为学生比赛是用Visual Studio 2013,所以我在自己的笔记本上安装了Visual Studio 2013(平常是用2010),安 ...
- 旧文备份:安装cygwin及在console下输入和显示中文
1.下载Cygwin.exe文件,双击安装,首先在"Choose A Download Source"的时候选择"Download Without Installing& ...
- 阿里数据库连接池druid
官方wiki: https://github.com/alibaba/druid/wiki 实用方法介绍的想当详细,包含监控.扩展.大力推荐!
- node-inspector调试工具使用方法
开发node.js程序使用的是javascript语言,其中最麻烦的还是调试,这里介绍一下node-inspector使用方法.具体资料可以看参考资料中的GITHUB文档. 工具/原料 node. ...
- windows 编译安卓iconv 库
由于NDK r15后,谷歌要统一将来的设备都要支持64位,而iconv只支持32位,后续的ndk都会去除iconv的支持,所以只能在iconv的官网下载源码编译库文件使用, 下载地址:https:// ...