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

Follow up:
Can you solve it without using extra space?

求链表是否有环的问题,要考虑链表为空的情况,定义一个快指针和一个慢指针,如果快指针和慢指针重合就表明有环

bool hasCycle(ListNode *head){
if(head == NULL || head->next == NULL) return false;
ListNode* first = head, *second = head;
while(second->next!=NULL && second->next->next!=NULL){
first = first->next;
second = second->next->next;
if(first == second) return true;
}
return false;
}

leetcode Linked List Cycle的更多相关文章

  1. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  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 单链表中的环

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

  4. [算法][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 ...

  5. 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 ...

  6. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  7. LeetCode: Linked List Cycle 解题报告

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

  8. LeetCode Linked List Cycle 解答程序

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

  9. [Leetcode] Linked list cycle ii 判断链表是否有环

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

  10. [LeetCode] Linked List Cycle II, Solution

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

随机推荐

  1. gzip

    gzip -c 将输出写到标准输出上,并保留原文本 gzip * : 把当前目录中的每个文件压缩成.gz文件 [root@NB gzip]# ls mysql-bin. mysql-bin..tar ...

  2. 重温WCF之群聊天程序(十)

    完成的效果图: 服务器端代码: using System; using System.Collections.Generic; using System.Linq; using System.Serv ...

  3. OCJP(1Z0-851) 模拟题分析(三)over

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  4. git push 使用总结

    git push命令用于将本地分支的更新,推送到远程主机.它的格式与git pull命令相仿. $ git push <远程主机名> <本地分支名>:<远程分支名> ...

  5. PHP保留小数位的三种方法

    /** * PHP保留两位小数的几种方法 * @link http://www.phpddt.com */ $num = 10.4567; //第一种:利用round()对浮点数进行四舍五入 echo ...

  6. [CentOS]安装软件:/lib/ld-linux.so.2: bad ELF interpreter解决

    转自:http://blog.csdn.net/wanglei2258/article/details/24961233 [CentOS]安装软件:/lib/ld-linux.so.2: bad EL ...

  7. 自己制作QQ空间音乐的具体方法

    1.打开QQ邮箱找到左栏下方的“文件中转站”--点击收藏文件--上传到收藏  将MP3或WMA音乐文件上传 上传完成点下载 下图:   2.点“保存”将最上面一排的地址全部复制  下图   3.为了更 ...

  8. maven项目Tomcat controller 404

    今天使用tomcat7.0.54启动现有的maven项目,可以正常启动,但是自己所写的所有的@controller注解的请求都报出了404的错误,在网上查了好久也很少找到这个问题,各种方法都尝试了也没 ...

  9. 1.CoreLocation 使用,获取当前位置

    1. ios7只要开始定位,系统就会自动要求你对应用程序授权 ios8之后,必须要代码中实现要求用户对应用程序授权 ,在plist中添加以下两个属性 NSLocationWhenInUseDescri ...

  10. 孙鑫VC学习笔记:多线程编程

    孙鑫VC学习笔记:多线程编程 SkySeraph Dec 11st 2010  HQU Email:zgzhaobo@gmail.com    QQ:452728574 Latest Modified ...