这个题真是坑死了,只怪自己不好吧。一开始审题,以为是给定一个首尾相连的链表,查看其中是否有循环(原谅我的无知吧!!)。然后在那写啊写啊写,还在纠结是局部循环还是前一半和后一半一样这样的循环,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的更多相关文章

  1. 【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  2. 【题解】【链表】【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  3. 【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 ...

  4. 【LeetCode】Linked List Cycle II(环形链表 II)

    这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? ...

  5. 【LeetCode】Linked List Cycle(环形链表)

    这道题是LeetCode里的第141道题. 题目要求: 给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? 简单题,但是还是得学一下这道题的做法,这道题是用双指针一个fast, ...

  6. 【Leetcode】【Medium】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 解题: ...

  7. 【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 ...

  8. 【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 ...

  9. 【Leetcode】【Medium】Linked List Cycle

    Given a linked list, determine if it has a cycle in it. 解题: 判断单链表是否具有环,使用两个指针once和twice遍历链表,once一次走一 ...

随机推荐

  1. FORM触发器执行顺序

    触发器执行顺序: 1. 当打开FORM时: (1) PRE-FORM (2) PRE-BLOCK(BLOCK级) (3) WHEN-NEW-FORM-INSTANCE (4) WHEN-NEW-BLO ...

  2. spring mvc 初始化错误

    java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.isPresent(Ljava/lang/String;Ljava/l ...

  3. 使用jna调用dll,jdk位数和dll位数的关系

    最近在学习jna,发现dll文件能能否成功调用取决于jdk位数. 32位jdk只能使用32位的dll,64位jdk只能使用64位的dll,否则位数不对应的话报的错是 "Exception i ...

  4. 第五百八十四天 how can I 坚持

    好累啊,不知道怎么搞的,也没干啥啊,学习学的..不会吧. 哎. 吃粉蒸吃的都快吃吐了,得休息段时间,哈哈 没想象中的那么简单啊,太难了,哎,怎么考,压力啊.哎,也没啥. 洗澡睡觉,先休息.

  5. 正则表达式python和C++对比

    pattern格式(基本通用): pattern格式 符号 说明 ^ 匹配开头 $ 匹配结尾 . 匹配任意一个字符 [...] 匹配任意一个指定的字符 [^...] 匹配任意一个非指定的字符 * 匹配 ...

  6. windows下安装多个tomcat服务

    摘要 公司服务器已经部署2个tomcat,分别属于不同的系统.今天新开发的系统也要上线测试,故新增一个tomcat服务器. 1.官网下载tomcat 7 解压缩版本.我使用的是 apache-tomc ...

  7. [原创]Matlab获取当前时间信息

    本文主要介绍下Matlab中如何获取当前时间的一些方法. 基本变量date.now.clock date 按照日期字符串返回当前系统时间 now 按照连续的日期数值返回当前系统时间 clock按照日期 ...

  8. .NET Remoting获取配置通道:

    接上文: public static string ChannelManagerUrl        {            get            {                retu ...

  9. Java写操作

    //:ThinkingInJava/net.mindview.io/write2File.java package net.mindview.io; import java.io.BufferedRe ...

  10. 理解AOP

    http://www.cnblogs.com/yanbincn/archive/2012/06/01/2530377.html Aspect Oriented Programming  面向切面编程. ...