题目: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. MTK Android Driver:GPIO

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2JrODYxMTEw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  2. 你有PSD的学位吗? - dp的日志 - 网易博客

    你有PSD的学位吗? - dp的日志 - 网易博客 你有PSD的学位吗? 2011-08-01 12:58:40|  分类: 感悟 |  标签:自我提升   |字号 大中小 订阅       去年, ...

  3. 服务器编程入门(11)TCP并发回射服务器实现 - 单线程select实现

    问题聚焦: 当客户端阻塞于从标准输入接收数据时,将读取不到别的途径发过来的必要信息,如TCP发过来的FIN标志. 因此,进程需要内核一旦发现进程指定的一个或多个IO条件就绪(即输入已准备好被读取,或者 ...

  4. Java学习文件夹

    每天进步一点点,先研究一门语言深入研究下去.

  5. php将中文插入数据库出现乱码

    通过php向mysql数据库插入数据,然后在数据库中查看的时候全是乱码(中文),但是取出之后放在页面上仍然正常.就是通过数据库查看的时候全是乱码不能阅读. mysql以UTF-8编码来保存中文,页面提 ...

  6. HDU 1241 :Oil Deposits

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  7. AlarmManager的学习与实现

    综述     这个类提供了一种使用系统提供的alarm服务.这个服务同意用户安排他们的应用程序在将来的某一个时间点执行.当设置的alarm响起,那么之前系统为这个alarm注冊的Intent就会自己主 ...

  8. ecshop首页调用指定分类的所有产品(指定一级调二级)

    第一种方法 第一 在/includes/lib_goods.php下增加如下代码,用过网上的直接换掉就可以 function index_get_cat_id_goods_best_list($cat ...

  9. 解决set /p yn= 接受键盘输入导致ECHO 处于关闭状态的问题

    今天写了一个自动更新程序的批处理脚本,但是有个变量一直赋值有问题.弄了一个下午终于找到原因及解决方法: ----转载要说明来自:博客园--邦邦酱好 哦 有问题的代码如下: @echo off echo ...

  10. Sql还原数据库出现3154错误

    要先知道还原的数据库的名字 然后在数据库中新建一个那个名字的数据库, his 是数据库的名字!! RESTORE DATABASE his FROM DISK = 'E:\RDHL文件\HIS相关文档 ...