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

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

若在while开始时判断fast==slow,会出现误判,即第一次循环时fast必定等于slow

 /**
* 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==NULL)
return false;
ListNode *slow=head,*fast=head;
while(fast!=NULL&&fast->next!=NULL){ fast=fast->next->next;
slow=slow->next;
if(fast==slow)
return true; }
return false;
}
};

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, returnnull. Follo ...

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

  3. 环形链表II 142 使用快慢指针(C++实现)

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

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

  5. [LeetCode题解]234. 回文链表 | 快慢指针 + 反转链表

    解题思路 找到后半部分链表,再反转.然后与前半部分链表比较 代码 /** * Definition for singly-linked list. * public class ListNode { ...

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

  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. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

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

  10. Linked List Cycle 判断一个链表是否存在回路(循环)

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

随机推荐

  1. python--前端CSS

    一.CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义了如何显示HTML元素,给HTML设置样式,让他更加美观. 当浏览器读到这个样式表, 他就会按照这个样式来对文档进行 ...

  2. python--线程的其他方法

    一 . current_thread的用法 import threading import time from threading import Thread, current_thread def ...

  3. mysqldump 常见报错及解决

    mysqldump失败案例及解决: 1.mysqldump: Error 2020: Got packet bigger than 'max_allowed_packet' bytes when du ...

  4. Java-在一个包装器对象中包装一个原始类型

    使用基本类型的包装对象,好处可以为空且可以序列化 package com.tj; public class MyClass2 { public static void main(String[] ar ...

  5. cookie小结(转)

    原文地址:http://www.cnblogs.com/xianyulaodi/p/6476991.html#_label0 作者:咸鱼老弟   阅读目录 什么是cookie       官方定义:N ...

  6. 论文《Piexel Recurrent Nerual Network》总结

    论文<Piexel Recurrent Nerual Network>总结 论文:<Pixel Recurrent Nerual Network> 时间:2016 作者:Aar ...

  7. [kubernetes] 使用 Minikube 快速搭建本地 k8s 环境 (基于 Docker 驱动模式)

    一.实验环境 操作系统:Centos 7 x86_64 Docker:1.12.6 二.部署 k8s 步骤 2.1  安装 kubectl cat <<EOF > /etc/yum. ...

  8. 【leetcode】lower_bound

    int binary_search(const vector<int>& stones,int val){ int sz=stones.size(); ,r=sz-; while( ...

  9. 设置好uTorrent让你的下载速度飞起来

    由于有会员反映下载国外种子速度很慢的问题,而我下同样的种子,竟然那天下载最高速度能到500K/秒.(我用的是移动的校园网,这种出了名的烂网,十天有七天是图片都打不开的网)这可见是所用软件和软件的设置问 ...

  10. Mysql 函数的应用

    CREATE TABLE `code_generate_dd` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT '主键', `first_code` ) NOT NU ...