【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一次走一 ...
随机推荐
- mac java目录
/Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk/Contents/Home mac java的安装目录为 /Library/Java/JavaVir ...
- Uiautomator自动编译运行脚本
Uiautomator的编译运行过程需要输入好几个命令,太麻烦. 花了点时间写了个简单的bat.方便多了.id输入当前使用的SDK ID号(android list target命令可以查看到),cl ...
- ffmpeg去logo<转>
用到 video filter —— delogo 通过周围像素插值去除 logo. 参数介绍: x y (必须)指定 logo 的坐标. w h (必须)指定 logo 的宽和高. band, t ...
- Using AFNetWorking 2.0 upload file to php web service server based on Slim
Recently i am developing the IOS app, a feature is needed to upload image to the webservice server. ...
- LINQ to SQL快速上手 step by step
Step1:建立数据库 在使用Linq to Sql前,我们要将相应的数据库建好.在这个Demo中,使用的数据库是SQL Server Express 2005. 我们首先建立一个 ...
- ServiceLocator 简单示例(转)
Service Locator Pattern in C#: A Simple Example(转) Service Locator Pattern in C# with Lazy Initializ ...
- 循序渐进之Spring AOP(5) - 创建切面
在掌握了可用的增强后,接下来要做的就是精确的描述切点.前面的示例都是指定一个目标类并把增强织入到所有方法中,实际开发显然会有更精细的筛选需求,比如对所有类中名称以test结尾的方法加入监控执行时间,或 ...
- python模块使用案例
python模块使用案例 一.使用MySQLdb模块代码示例: # 导入 MySQLdb模块 import MySQLdb # 和服务器建立链接,host是服务器ip,我的MySQL数据库搭建在本机, ...
- 在Linux下使用RAID--使用mdadm工具创建软件Raid 0(1)
在Linux下使用RAID--使用mdadm工具创建软件Raid 0(1) RAID即廉价磁盘冗余阵列,其高可用性和可靠性适用于大规模环境中,相比正常使用,数据更需要被保护.RAID是一些磁盘的集合, ...
- Bash Shell实用快捷键
Ctrl-D 相当于Del键,即删除光标所在处的字符 Ctrl-E 相当于End键,即将光标移动到本行末尾 Ctrl-K 用于删除从光标处开始到结尾处的所有字符 Ctrl-L 清屏,相当于clear命 ...