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

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

思考:快慢指针,快指针一次走两步,慢指针一次一步。若快指针跟慢指针指向同一个结点,则有环。若快指针到达链表末尾即指向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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(!head||!head->next) return false;
ListNode *p=head;
ListNode *q=p->next;
while(q&&q->next)
{
if(p==q) return true;
else
{
q=q->next->next;
p=p->next;
}
}
return false;
}
};

  

[LeetCode]Link List Cycle的更多相关文章

  1. [LeetCode]Link 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 II 和I 通用算法和优化算法

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

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

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

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

  5. [LeetCode]Linked List Cycle II解法学习

    问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...

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

  7. LeetCode——Linked List Cycle II

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

  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 & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

随机推荐

  1. ZigBee profile

        每个ZigBee设备都与一个特定模板相关联,可能是公共模板或私有模板.这些模板定义了设备的应用环境.设备类型以及用于设备间通信的簇.采用公共模板,可以确保不同供应商的设备在相同应用领域的互操作 ...

  2. 链接与ELF文件格式的复习

    在这里复习一下链接的知识: 什么是链接(linking):把源代码形成的模块独立编译后组装成一个整体的的过程叫做链接. 链接主要过程包括:地址和空间分配(address and storage all ...

  3. Git的学习总结和使用时遇到的问题。

                        git 是一款非常强大的版本控制工具,现在市场占有率应该是一家独大了,以前用svn的童鞋估计都转投git阵营了吧   加上很多公司也用git管理自己的项目,所以 ...

  4. jQuery增加删除修改tab导航特效

    HTML:         <div class="container iden_top">                <ul>             ...

  5. How to force to Fullscreen Form

    Is it possibile by code resize a form to fullscreen? (like button Maximize) ? // VAR Changed on 10 J ...

  6. sqlchemy - day3

         session 直接上代码,创建表结构,初始化部分数据. from sqlalchemy import create_engine engine = create_engine(" ...

  7. 图解 CSS: 理解样式表的逻辑(转载)

    原文:http://www.cnblogs.com/del/archive/2009/02/01/1382141.html 样式表可以是外部的.内联的或嵌入的; 链接外部样式文件一般是:<lin ...

  8. Java集合的小抄

    在尽可能短的篇幅里,将所有集合与并发集合的特征.实现方式.性能捋一遍.适合所有"精通Java",其实还不那么自信的人阅读. [转自:花钱的年华] 期望能不止用于面试时,平时选择数据 ...

  9. Spark Standalone运行过程

    以下内容参考http://www.cnblogs.com/luogankun/p/3912956.html 一.集群启动过程--启动Master 二.集群启动过程--启动WorkerWorker运行时 ...

  10. mac下wget用来仿站

    wget -c -r -np -k -L -p http://www.domain.com 参考 http://www.v2ex.com/t/147870