题意:给一个单链表,判断其是否出现环!

思路:搞两个指针,每次,一个走两步,另一个走一步。若有环,他们会相遇,若无环,走两步的指针必定会先遇到NULL。

 /**
* 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) {
if(!head) return false;
ListNode * one=head, *two=head->next;
while(two && two->next && one!=two)
{
one=one->next;
two=two->next->next;
}
if(two&&two->next) return true;
else return false;
}
};

AC代码

LeetCode Linked List Cycle 单链表环的更多相关文章

  1. [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 ...

  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. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

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

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

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

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

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

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

  7. 每天一道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 ...

  8. 【Algorithm | 链表】单链表“环”、“环的起点”、“环的长度”问题

    参考资料 • Floyd判圈算法 { 链接 } • 单链表“环”.“环的起点”.环的长度”问题 { 链接 } 链表环的问题 一.判断链表有换 使用两个指针slow和fast.两个指针开始时均在头节点处 ...

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

随机推荐

  1. matlab中函数fscanf

    matlab中函数fscanf matlab中函数fscanf在文件读取方面的实例如下: 从文件中有格式地读数据 fscanf 语法1:[a,count]=fscanf(fid,format,size ...

  2. 【WCF--初入江湖】08 并发与实例模式

    08 并发与实例模式 1. 实例上下文模式   一个服务代理:servicePoxy ChannelFactory<IService1> factoryservicel = new Cha ...

  3. hdoj 1596 find the safest road

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1596 分析:Dijkstra变体,最短路径判断计算方式:Safe(P) = s(e1)*s(e2)…* ...

  4. [shell编程]初识sed和gawk

    一.sed编辑器       shell脚本最常见的用途就是处理文本文件,sed和gawk能够极大的简化需要进行的数据处理任务.sed编辑器是流编辑器,跟普通交互式文本编辑器(如vim)不同.流编辑器 ...

  5. Javascript 中childNodes和children的区别

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. 用supervisor控制celery时的脚本

    注意启停的先后顺序及判断即可. 这样,在更改task之后,要重启就方便很多啦.. #!/bin/sh supervisord_count=`ps -elf | grep celery | grep - ...

  7. Oracle 10 - 数据库表

    Oracle数据库表类型 1.堆表 2.索引组织表 3.索引clustered表 4.散列clustered表 5.有序散列clustered表 6.嵌套表 7.临时表 8.对象表 9.外部表 Ora ...

  8. 原版win7镜像IE主页被篡改?

    装了几次系统,镜像是从“MSDN我告诉你”上下载的,但是每次装完,发现IE主页不是microsoft的官方网页,而是www.3456.com. 这就奇了怪了,难道“MSDN我告诉你”提供的不是原版镜像 ...

  9. 【Linux高频命令专题(12)】touch.md

    概述 一般在使用make的时候可能会用到,用来修改文件时间,或者新建一个不存在的文件. 命令格式 touch [选项]... 文件... 命令参数 -a 或--time=atime或--time=ac ...

  10. SQLite操作(C# )

    C#连接SQLite的...方法 http://www.cnblogs.com/virusswb/archive/2010/09/17/SQLite1.html 1 SQLite简介 SQLite,是 ...