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

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

判断一个链表是否存在环,维护快慢指针就可以,如果有环那么快指针一定会追上慢指针,代码如下:

 class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode * slow, * fast;
slow = fast = head;
while(slow && fast){
slow = slow->next;
fast = fast->next;
if(fast) fast = fast->next;
if(fast && slow && slow == fast)
return true;
}
return false;
}
};

LeetCode OJ:Linked List Cycle(链表循环)的更多相关文章

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

  2. [LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  3. [LeetCode] 142. Linked List Cycle II 链表中的环 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 II

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

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

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

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

  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 141. Linked List Cycle 判断链表是否有环 C++/Java

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

  9. (链表 双指针) leetcode 142. Linked List Cycle II

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

  10. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

随机推荐

  1. SQL逻辑查询语句 执行顺序 语法顺序

    SELECT语句语法顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN < ...

  2. SQL基础二

    一.SQL SELECT 语句 SELECT 语句用于从表中选取数据.结果被存储在一个结果表中(称为结果集). SQL SELECT 语法: SELECT 列名称 FROM 表名称 以及: SELEC ...

  3. day13 迭代器

    迭代器 'iterable' 可迭代的 内部含有__iter__方法的数据类型就是可迭代的 —— 可迭代协议 print(dir([])) print(dir({})) print(dir(5)) p ...

  4. Node.js API学习笔记(二)

    本文发表于本人博客. 上一节笔记说到创建Buffer实例,这节继续讲Buffer.本节讲解下Buffer的一些静态方法.写入以及读取方法. Buffer.isEncoding(编码)判断nodejs是 ...

  5. CNN学习笔记:目标函数

    CNN学习笔记:目标函数 分类任务中的目标函数 目标函数,亦称损失函数或代价函数,是整个网络模型的指挥棒,通过样本的预测结果与真实标记产生的误差来反向传播指导网络参数学习和表示学习. 假设某分类任务共 ...

  6. linux svn 命令

    windows下的TortoiseSVN是资源管理器的一个插件,以覆盖图标表示文件状态,几乎所以命令都有图形界面支持,比较好用,这里就不多说.主要说说linux下svn的使用,因为linux下大部分的 ...

  7. Java技术学习路线

    转载 作者:David 链接:https://www.zhihu.com/question/25255189/answer/86898400来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商 ...

  8. centos6.5系统python2.6升级到python3.6

    1.安装必备的工具 wget:yum install wget gcc:yum install gcc zlib zlib-devel: yum install zlib zlib-devel -y ...

  9. SqlHelper简单实现(通过Expression和反射)7.MySql数据处理类

    MySql的数据处理类和Sql Server没有太大差别,从思路上来说没有区别,所以此处只是给出代码,不再多加解释了. using System; using System.Configuration ...

  10. jQuery单选多选按钮选中美化特效

    在线演示 本地下载