leetcode -- Linked List Cycle -- 重点
2015-12-09 20:46
405 查看
https://leetcode.com/problems/linked-list-cycle/
参考/article/4982512.html
一开始我错误的code:
正确的code:
参考/article/4982512.html
一开始我错误的code:
class Solution(object): def hasCycle(self, head): """ :type head: ListNode :rtype: bool """ if not head : return False slow, fast = head, head while fast and fast.next and fast != slow:#这里这个条件fast!=slow,使得在一开始就无法进入循环 slow = slow.next fast = fast.next.next if fast == None or fast.next == None: return False else: return True
正确的code:
class Solution(object): def hasCycle(self, head): """ :type head: ListNode :rtype: bool """ if not head : return False slow, fast = head, head while fast.next and fast.next.next:#这里用fast and fast.next也可以 slow = slow.next fast = fast.next.next if slow == fast: return True return False
相关文章推荐
- 关于U3D贴图格式压缩
- [ahk]爬虫基础 post数据获取网站上的图片(获取艺术签名)
- Unity3D抗锯齿设置
- Java之JDOM生成XML和解析
- 《实例化需求》第一篇阅读体会
- ListView之setEmptyView的问题
- Node.js使用过程中jade,mongo命令使用错误
- 【CF505D】Mr. Kitayuta's Technology
- html————表格
- OpenGL的安装与配置
- UVALive 6187 Never Wait for Weights 带权并查集
- P53 T5
- C#中关闭第一个Form窗口,打开另一个新窗口方法
- Trafic
- Notes
- NuGet发布
- c语言的笔记
- 判断101-200之间有多少个素数(又称质数(prime number)),并输出所有素数。
- 关于为什么保存Transform等引用效率会更高
- IOS 的验证码倒计时