【leetcode】Linked List Cycle
这个题真是坑死了,只怪自己不好吧。一开始审题,以为是给定一个首尾相连的链表,查看其中是否有循环(原谅我的无知吧!!)。然后在那写啊写啊写,还在纠结是局部循环还是前一半和后一半一样这样的循环,blah blah....,此处省略各种YY。最后发现其实就是给定一个链表的头,判断这个链表是否环!
1.用while循环,如果碰到next指向null,则返回False。这种做法说得通,但是前提是你也得有null才行啊,如果是个环,就是个无限循环啊!
2.一个指针不行,那就用2个指针,主要就是解决上面无限循环的情况。因为一个指针会出现无限循环的情况,那么我两个指针,一个步伐大一点,一个步伐小一点,那么,如果有环的话,经过有限次循环,他们肯定会有重叠的一天,即走得快的把走得慢的套了圈。于是,就变得很简单了,代码如下:
class Solution:
# @param head, a ListNode
# @return a boolean
def hasCycle(self, head):
if(not head or not head.next): #if list is null or only have one
return False
i = head
j = head.nextwhile(i and j and j.next):
i = i.next;
j = j.next.next
if(i==j):
return True
return False
需要注意的是要考虑到特殊情况:1.只有一个节点,且自成环;2.这个列表本身是空的。这些都是一个健壮的算法应该考虑到的!
【leetcode】Linked List Cycle的更多相关文章
- 【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 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【leetcode】Linked List Cycle II (middle)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【LeetCode】Linked List Cycle II(环形链表 II)
这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? ...
- 【LeetCode】Linked List Cycle(环形链表)
这道题是LeetCode里的第141道题. 题目要求: 给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? 简单题,但是还是得学一下这道题的做法,这道题是用双指针一个fast, ...
- 【Leetcode】【Medium】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 解题: ...
- 【LeetCode】【C++】Linked list cycle 2
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【LeetCode】【Python】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】【Medium】Linked List Cycle
Given a linked list, determine if it has a cycle in it. 解题: 判断单链表是否具有环,使用两个指针once和twice遍历链表,once一次走一 ...
随机推荐
- js动画性能提升笔记
JavaScript动画的性能并不亚于CSS动画.因此,如果使用了现代的动画库,例如Velocity,那么动画引擎的性能将不再是app的瓶颈,构成瓶颈的只有代码. 网络性能相关 动画是浏览器运行中资源 ...
- 第三章 数组与字符串 UVa1588 Kickdown
题目要求简述:给定长度分别为n1,n2(n1,n2<=100)且每列的高度只为1或者2的长条.需要将他们放入一个高度为3的容器,问能够容纳它们的最短容器长度. 分析: 对于这样的题目显而易见有两 ...
- Provisional headers are shown,本地测试成功,服务器运行却失败
基于MVC的项目 具体情况是一个页面在访问的时候进不了首页,但详细页面却可以进去 下面说说解决方法和思路,以便找出问题所在 第一:把服务器代码下载到本地运行,代码是否出错,出错了,问题找到了,没出错接 ...
- ITop
iTop,即IT运营门户(IT Operation Portal),是一个开源web应用程序,用于IT环境的日常运营.它基于ITIL最佳实践,而又不拘泥于任何具体流程.它很灵活,可以适应不管是非正 式 ...
- eclipse 删除所有注释及空白行
Ctrl+F 删除java注释: /\*{1,2}[\s\S]*?\*/ Ctrl+F 删除xml注释: <!-[\s\S]*?--> Ctrl+F 删除空白行: ^\s*\n 选 ...
- 《深入理解Nginx》阅读与实践(一):Nginx安装配置与HelloWorld
最近在读陶辉的<深入理解Nginx:模块开发与架构解析>,一是想跟着大牛练练阅读和编写开源代码的能力,二是想学学Nginx优秀的架构设计,三是想找一个点深入下Linux下网络编程的细节.侯 ...
- HDU 5410 CRB and His Birthday(完全背包变形)
CRB and His Birthday Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- Windows下使用性能监视器监控SqlServer的常见指标
这篇文章主要介绍了Windows下使用性能监视器监控SqlServer的常见指标,常见指标包括Buffer Cache Hit Ratio.Pages/sec. Available Bytes.Dis ...
- 2015年可用的TRACKER服务器大全
udp://tracker.openbittorrent.com:80/announceudp://tracker.publicbt.com:80/announcehttp://pubt.net:27 ...
- Tlist
Tlist (Classes.pas) 在我刚开始接触TList的时候,TList搞得我迷雾重重,都是Capacity属性惹的祸.我查了Delphi的帮助,它说Capacity是TList的最大容量, ...