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

思路:搞两个指针,每次,一个走两步,另一个走一步。若有环,他们会相遇,若无环,走两步的指针必定会先遇到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. PHP之cookie相关实例教程与经典代码

    ·php 中cookie和session的用法比较 ·php会话控制cookie与Session会话处理 ·php中利用cookie实现购物车实例 ·php中cookie与session应用学习笔记 ...

  2. uva 11181

    直接枚举计算就行: #include<cstdio> #include<cstring> #include<algorithm> #define maxn 22 u ...

  3. java Hotspot 内存管理白皮书(中文翻译)

    转自: http://my.oschina.net/u/568779/blog/166891 1引言 一个健壮的 Java™2平台,Standard Edition (J2SE™)拥有一个自动内存管理 ...

  4. Sublime Text 3 搭建 React.js 开发环境

    sublime有很强的自定义功能,插件库很庞大,针对新语言插件更新很快,配合使用可以快速搭建适配语言的开发环境. 1. babel 支持ES6, React.js, jsx代码高亮,对 JavaScr ...

  5. java基础知识回顾之java Thread类学习(七)--java多线程通信等待唤醒机制(wait和notify,notifyAll)

    1.wait和notify,notifyAll: wait和notify,notifyAll是Object类方法,因为等待和唤醒必须是同一个锁,不可以对不同锁中的线程进行唤醒,而锁可以是任意对象,所以 ...

  6. TVM 下载监控

    iftop -n -i em1 -F 218.26.188.220/32 到10.10.81.23上用上面的语句看山西的流量. 到/opt/data/log/下看日志 到/opt/data/tvmca ...

  7. 小米2S 连接Ubuntu Android Studio

    1. 首先打开手机上的开发者选项,USB调试.拨号:*#*#717717#*#*  ,手机会以Toast形式出现“……enable”字样.再次拨号可disable. 2. Ubuntu安装mtpfs: ...

  8. NOIP 2015提高组复赛

    神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1写在第 ...

  9. Project Euler 82:Path sum: three ways 路径和:3个方向

    Path sum: three ways NOTE: This problem is a more challenging version of Problem 81. The minimal pat ...

  10. SULogger:iOS日志可视化工具

    前言 debug对于咋们程序员来说家常便饭,但有时候我们会遇到一种情况:开发某个功能时,需要在某个特定场景下进行调试,而这个场景并没有MacBook来进行连接debug,偏偏我们需要获得调试时的一些信 ...