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) {
if(!head)
return false;
ListNode* fast = head->next;
ListNode* slow = head;
while(fast) {
if(fast == slow)
return true;
if(fast->next&&fast->next->next){//短路的技巧
fast = fast->next->next;
slow = slow->next;
}
else
return false;
}
return false;
}
};

  设置两个指针,一个快指针,一个慢指针。快指针每次走两步,慢指针每次走一步,如果相遇就是有环。

  19行先判断快指针能不能走1步,如果能在判断2步之后是不是链表末尾,如果不是末尾就可以向下走。

==============================我是分割线=============================================

  《编程之美》上看到一道题目,判断两个两个链表(无环)是否相交,可以转化为这题的解法,把第二个链表头接到另一个的末尾,在检测是否有环,有环就是相交。

  两一种解法更简单,比较倒数第二节链表是否相等。越来愈感受到算法的美妙了.QAQ

Linked List Cycle的更多相关文章

  1. [LeetCode] Linked List Cycle II 单链表中的环之二

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

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

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

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

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

  7. [CareerCup] 2.6 Linked List Cycle 单链表中的环

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

  8. Java for LeetCode 142 Linked List Cycle II

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

  9. 【题解】【链表】【Leetcode】Linked List Cycle II

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

  10. 3月2日 Linked List Cycle

    今天星期天,准备好周一的PPT内容,再回来做题,以后考虑周末做一个APP或者微信帐号玩吧. 回到题目, Linked List Cycle,一个检查单项链表是否有环路的问题. 题目周五的时候就简单做过 ...

随机推荐

  1. ExtJS4图片验证码的实现

    ExtJS4学习笔记(十)---ExtJS4图片验证码的实现 转自:http://blog.sina.com.cn/s/blog_8d4bbd890100xaxh.html     上多少篇文章,重要 ...

  2. hadoop pipes wordcount compile

    http://devel.cs.stolaf.edu/projects/bw/wiki.real/index.php/Hadoop_Reference,_January_2011 http://guo ...

  3. python-set集合类方法

    s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])返回的是{11,22,33,44,'Tom','tony',77,2.5}(注意:返回的并不是一个字典,只是告 ...

  4. ACdrea 1217---Cracking' RSA(高斯消元)

    ACdrea  1217---高斯消元 Description The following problem is somehow related to the final stage of many ...

  5. Android5.0新特性——全新的动画(animation)

    全新的动画 在Material Design设计中,为用户与app交互反馈他们的动作行为和提供了视觉上的连贯性.Material主题为控件和Activity的过渡提供了一些默认的动画,在android ...

  6. FIM 2010: Kerberos Authentication Setup

    The goal of this article is to provide some background information regarding the Kerberos related co ...

  7. 【读书笔记】iOS-GCD-系统提供的dispatch方法

    系统提供的dispatch方法如下: //系统提供的dispatch方法 //后台执行: dispatch_async(dispatch_get_global_queue(0, 0), ^{ // s ...

  8. OC中代理的使用

    通常代理的使用需要以下几个步骤: 1.制定协议.协议可以在委托对象的.h中声明,也可以在单独的.h中声明.制定协议后,在协议中声明需要代理对象来实现的方法. 2.设置代理属性.制定协议后需要为委托对象 ...

  9. 全球最低功耗蓝牙单芯片DA14580的硬件架构和低功耗

    号称全球最低功耗蓝牙单芯片DA14580在可穿戴市场.健康医疗.ibeacon定位等市场得到广泛的应用,但是因为其较为封闭的技术/资料支持导致开发人员有较高的技术门槛,网络上也极少看到有关DA1458 ...

  10. Java 实现多线程的三种方式

    import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; public class Main { pu ...