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

    TListView 的 Header 部分默认 BtnFace 颜色,高度也不能改变.我们可以通过编写一些代码来实现这些功能: 获得TListView 的Header 的句柄: TListView的H ...

  2. javascript 函数执行上下文

    在js里,每个函数都有一个执行的上下文,我们可以通过this来访问. 如: 全局函数 function test(){ var local = this; } 我们发现local等于window(do ...

  3. PHP中,文件上传实例

    PHP中,文件上传一般是通过move_uploaded_file()来实现的.  bool move_uploaded_file ( string filename, string destinati ...

  4. 从Unity引擎过度到Unreal4引擎(最终版)

    原文地址:http://demo.netfoucs.com/u011707076/article/details/44036839 前言 寒假回家到现在已经有十多天了,这些天回家不是睡就是吃....哎 ...

  5. SAP无损耗,FP前台和回写均有2%损耗

    SAP前台显示无损耗 FP前台显示有损耗 回写也有损耗 检查:从SAP取数到FP表是没有损耗 1132物料编码的主数据也是没有损耗 检查:FP_MO2SAP存储过程

  6. ibatis XML标签的含义

    selectKey: 有3个主要的属性:     1)resultClass:返回的主键的数据类型,跟sqlMap中的数据类型一致:     2)type:表示主键在insert之前或之后生成(取决于 ...

  7. jQuery ajax - getScript() 方法

    通过 AJAX 请求来获得并运行一个 JavaScript 文件: HTML 代码: <button id="go">Run</button> <di ...

  8. 【C#】 一些不常用,很容易混淆的知识点

    [C#] 一些不常用但很容易混淆的知识点 1. 访问修饰符 internal ,译为内部的, 在同一个程序集中可访问,它的内部是相对与程序集的,可不能想当然了 2. String.Compare 这个 ...

  9. 【Python全栈笔记】00 12-14 Oct Linux 和 Python 基础

    Linux 基础认识 更加稳定,安全,开源 设置好ssh协议后可以通过windows系统连接Linux,基于ssh协议进行通信 '/' 为根目录 cd / -> 切换到根目录 ls -lh 列出 ...

  10. g++与c++扩栈方法

    g++: /* * Problem: * Author: SHJWUDP * Created Time: 2015/8/5 星期三 15:54:42 * File Name: tmp.cpp * St ...