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. linux c编程:互斥锁

    们常说互斥锁保护临界区,实际上是说保护临界区中被多个线程或进程共享的数据.互斥锁保证任何时刻只有一个线程在执行其中的代码. 互斥锁具有以下特点: ·原子性:把一个互斥锁定义为一个原子操作,这意味着操作 ...

  2. 新手入门:java文件转成jar包再转成exe文件——图文教程

    [本文简介] 由于课程设计的原因,研究着如何把java 程序转成exe,最终成功了,现在把过程记录分享一下. 本文将介绍如何把一个跑在eclipse的java应用,导出成jar文件,再变成exe可执行 ...

  3. Python函数加工厂-装饰器

    引言: 函数和装饰器好比程序界的加工厂: 1.函数一般可用来加工一种或者多种数据类型的数据:字符串.数字.列表.字典等 举一个简单例子:已知半径求面积 def s(r): s = 3.14 * r * ...

  4. clipboard

    我们在网页上放置一个复制按钮,主要用来方便用户复制链接之类的复杂文本,以往的做法是,通过JS依靠Flash,甚至借助jQuery庞大的js库来实现文本复制到剪贴板的.今天我要给大家介绍的是一款极现代的 ...

  5. 优秀 H5 案例收集 Vol.2(不定期更新)

    上期浏览:Vol.1 再见了,影史最性感的硬汉http://news.163.com/special/fdh5_wolverine/ 活出真我http://balfhcy.pernod-ricard- ...

  6. MySQL Binlog解析(1)

    一.Binlog File Binlog files start with a Binlog File Header followed by a series of Binlog Event Binl ...

  7. MySQL-5.7 存储过程及函数

    1.语法 CREATE [DEFINER = { user | CURRENT_USER }] PROCEDURE sp_name ([proc_parameter[,...]]) [characte ...

  8. sublime批量替换文本重复单词

    事先需要把单词打到文本的每一行 排序 按F9或者选择菜单:Edit > Sort Lines,对每行文本进行排序 查找重复行 排序好后,按Ctrl+F,调出查找面板 查找字符串: ^(.+)$[ ...

  9. Linux系统下使用pwgen生成密码的使用教程

    pwgen生成的密码易于记忆且相当安全.从技术上来说,容易记忆的密码不会比随机生成的密码更加安全.但是,在大多数情况下,pwgen生成的密码已经足够安全,除了网银密码等需要高安全等级的情况外.使用易于 ...

  10. maven项目包结构

    groupId填写公司名称,如com.enn.ennewartifactId填写项目名称webapps如父工程: <groupId>com.enn.ennew</groupId> ...