题目:Given a linked list, determine if it has a cycle in it.

  判断一个链表是否有环

代码:

  

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *fast(head), *slow(head);
while(fast && fast->next)
{
fast = fast->next->next;
slow = slow->next;
if(fast == slow)
return true;
}
return false;
}
};

关于进一步求环的长度和入口结点可以看另一篇博客http://www.cnblogs.com/zhangbaochong/p/5151986.html

[LeetCode141]Linked List Cycle的更多相关文章

  1. LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

    链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

  2. 每天一道LeetCode--141.Linked List Cycle(链表环问题)

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  3. LeetCode141:Linked List Cycle

    题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin ...

  4. [LeetCode] Linked List Cycle II 单链表中的环之二

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

  5. [LeetCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  6. [LintCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...

  7. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  8. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  9. 15. Linked List Cycle && Linked List Cycle II

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...

随机推荐

  1. ActiveReports 9 新功能:创新的设计分层报告

     在最新的ActiveReports 9报表控件添加了几个新功能,为了帮助您创建一个漂亮的外观在较短的时间内.强大的报表系统.本文重点讨论创新的分层设计报告,分组报告内容管理和设计,于实现报表套打 ...

  2. Ubuntu9.04更新源

    1.sudo gedit /etc/apt/sources.list 编辑你的源列表,加入以下列表中你认为适合你的,拷贝到你的列表中,然后保存列表. Archive.ubuntu.com更新serve ...

  3. 在Windows下搭建React Native Android开发环境

    widows版本: win7 64位 专业版 1. 安装jdk.(我用的jdk7) 注意选择x86还是x64版本, 添加到系统PATH环境变量 2. 准备好android sdk 这个不多说,同时推荐 ...

  4. www.centos.org

    https://www.centos.org/forums/viewtopic.php?t=5770 In order to conserve the limited bandwidth availa ...

  5. java,spring,tomcat,跨域设置

    通过工具远程访问rest会出现  已阻止跨源请求:同源策略禁止读取位于 http://XXXXX:1200/gyly/busData 的远程资源.(原因:CORS 请求失败).  修改方法 添加Fil ...

  6. SQL语句查询数据库的触发器、存储过程、视图以及表的SQL语句

    Sql Server数据库用SQL语句查询方法如下: select name from sysobjects where xtype='TR' --所有触发器 select name from sys ...

  7. C#的百度地图开发(四)前端显示与定位

    原文:C#的百度地图开发(四)前端显示与定位 有了这些定位信息,那要如何在前端的页面上显示出来呢?这需要用到百度地图的JavaScript的API.下面是示例代码. 前端代码 <%@ Page  ...

  8. CentOS 7 安装MySql Server 5.6

    1. 安装MySql Server 在/etc/yum.repos.d/目录下添加以下文件mysql-community.repo文件,内容如下: [mysql56-community] name=M ...

  9. 13.怎样自学Struts2之Struts2本地化[视频]

    13.怎样自学Struts2之Struts2本地化[视频] 之前写了一篇"打算做一个视频教程探讨怎样自学计算机相关的技术",优酷上传不了,仅仅好传到百度云上: http://pan ...

  10. hdu 4454 Stealing a Cake(三分法)

    给定一个起始点,一个矩形,一个圆,三者互不相交.求从起始点->圆->矩形的最短距离. 自己画一画就知道距离和会是凹函数,不过不是一个凹函数.按与水平向量夹角为圆心角求圆上某点坐标,[0, ...