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

    我们先准备mysql数据表,假设项目中有一张记录学生信息的表student,并有id,name,sex,age分别记录学生的姓名.性别.年龄等信息. CREATE TABLE `student` ( ...

  2. .NET3.5中JSON用法以及封装JsonUtils工具类

    .NET3.5中JSON用法以及封装JsonUtils工具类  我们讲到JSON的简单使用,现在我们来研究如何进行封装微软提供的JSON基类,达到更加方便.简单.强大且重用性高的效果. 首先创建一个类 ...

  3. python生成中文验证码,带旋转,带干扰噪音线段

    # -*- coding: utf-8 -*- """ Created on Sun Oct 4 15:57:46 2015 @author: keithguofan & ...

  4. XidianOJ 1172 Hiking

    题目描述 BlacKin and GKCY are going hiking together. Besides their personal items, there are some items ...

  5. MySQL_财务统计各产品品类各城市上周收入毛利表_20161202

    注:财务需要统计各产品品类各城市上周毛利情况 下面这样的表是沟通后展现的形式.数据展现形式我认为的大都是行列转 列转行的转置 从财务角度这样展现形式比较适合. 由于黄色部分为汇总项目,因此我拆分成9个 ...

  6. asp.net下拉列表绑定数据库多个字段

    select ID, CONVERT(varchar(10),TBName) +','+RoomName+ ',最大人数:'+CONVERT(varchar(10),MAXNum) as ClassR ...

  7. 这是一个比较清晰的SSH框架结构,有兴趣的博友可以看一下

    http://www.cnblogs.com/zhaowancheng/p/5841591.html

  8. JAVA学习<四>

    一. 数组: Java 中操作数组只需要四个步骤: 1. 声明数组 语法:  数据类型[ ] 数组名: 或者   数据类型 数组名[ ]: 其中,数组名可以是任意合法的变量名. 2. 分配空间 简单地 ...

  9. 开源PLM软件Aras详解四 ItemType的概念

    首先,我们需要了解什么是ItemType,俗称对象类 官方一点就是ItemType是一个用来定义业务对象类的业务对象类 通过ItemType定义的每个业务对象类可以产生各自的实例对象 通过ItemTy ...

  10. 在Linux下安装配置phpMyAdmin步骤

    本文于CentOS 6.5下测试可行,欢迎转载和指正. phpMyAdmin是一种基于Web的免费MySQL管理工具.phpMyAdmin是用PHP编写的,因此只有支持PHP的服务器才能使用phpMy ...