题目: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. c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)

    一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index ...

  2. FZU2177(dp)

    传送门:ytaaa 题意:有n个***(不能调换顺序),可以组成x(x<n)个炸弹,每个炸弹的威力为该组的(max-min)^2,现在给出n个***的威力值,求能组成所有炸弹的最大威力和. 分析 ...

  3. nyist oj 311 全然背包 (动态规划经典题)

    全然背包 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 直接说题意,全然背包定义有N种物品和一个容量为V的背包.每种物品都有无限件可用.第i种物品的体积是c,价值是 ...

  4. cxf和jboss eap 6.2版本号冲突

    升级jboss版本号到jjboss-eap-6.2之后,启动项目时CXF出现异常. 在jboss-as-7.1.1.Final.apache-tomcat-7.0.37以及jboss-eap-6.1 ...

  5. SWFUpload简单使用样例 Java版(JSP)

    SWFUpload官方的样例都是PHP的,在这里提供一个Java版的最简单的使用样例,使用JSP页面完毕全部操作. 实现上传,分为三步: 1.JavaScript设置SWFUpload部分(与官方样例 ...

  6. sql语句查询数据库中的表名/列名/主键/自动增长值

    原文地址:http://blog.csdn.net/pukuimin1226/article/details/7687538 ----查询数据库中用户创建的表 ----jsj01 为数据库名 sele ...

  7. 寻找失踪的整数数组(Find the missing integer)

    排列a包含N分子,其元素属于[0,N]之间,且不存在反复的元素.请你找出数组中缺失的元素(由于[0,N]之间有N+1个元素.而数组仅仅能存储N个元素.所以必定缺少一个元素).当中对数组的操作满足下列的 ...

  8. Macosx Setdns

    通过C语言接口在Mac App内部对系统的DNS配置进行改动. Mac OS X设置DNS代码 演示样例代码setDNS.c内容例如以下: #include <SystemConfigurati ...

  9. java移位运算符具体解释

    java移位运算符不外乎就这三种:<<(左移).>>(带符号右移)和>>>(无符号右移). 1. 左移运算符 左移运算符<<使指定值的全部位都左移 ...

  10. linux设备驱动程序第四部分:从如何定位oops对代码的调试方法,驱动线

    在一个我们谈到了如何编写一个简单的字符设备驱动程序,我们不是神,编写肯定会失败的代码,在这个过程中,我们需要继续写代码调试.在普通c应用.我们经常使用printf输出信息.或者使用gdb要调试程序,然 ...