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

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

C++

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

linked-list-cycle leetcode C++的更多相关文章

  1. 141. Linked List Cycle - LeetCode

    Question 141. Linked List Cycle Solution 题目大意:给一个链表,判断是否存在循环,最好不要使用额外空间 思路:定义一个假节点fakeNext,遍历这个链表,判断 ...

  2. Linked List Cycle——LeetCode

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

  3. Linked List Cycle leetcode II java (寻找链表环的入口)

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

  4. Linked List Cycle leetcode java (链表检测环)

    题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin ...

  5. Linked List Cycle - LeetCode

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

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

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

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

  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 && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  10. [LeetCode] 141&142 Linked List Cycle I & II

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

随机推荐

  1. 使用 elementUI 的表单进行查询,表单中只有一个文本框时,回车会自动触发表单的提交事件,导致页面的刷新。

    使用elementUI的el-form组件进行查询时,当输入框仅有一项时,回车自动提交表单,浏览器会刷新页面: 原因:由于当表单只有一个文本框时,按下回车将会触发表单的提交事件, 从而导致页面刷新. ...

  2. Jmeter扩展组件开发(10) - 自定义扩展函数助手的开发

    CODE package com.functions;import org.apache.jmeter.engine.util.CompoundVariable;import org.apache.j ...

  3. YOLO v3 & Pascal VOC数据集

    代码地址:https://github.com/YunYang1994/tensorflow-yolov3 https://hackernoon.com/understanding-yolo-f5a7 ...

  4. ~/.emacs emacs 配置文件

    windows ~/.emacs (when (>= emacs-major-version 24) (require 'package) (add-to-list 'package-archi ...

  5. Appium Android Toast控件

    Android Toast控件是Android系统级别的控件,不是App的控件,getPageSource是⽆法找到的. Toast介绍 1.背景 在安卓设备里面,使用各种手机应用程序的时候,需要先进 ...

  6. ubuntu 安装 gightingale

    ubuntu 安装 nightingale 准备情况 # 三台ubuntu机器 192.168.1.91 master 192.168.1.92 node1 192.168.1.93 node2 # ...

  7. position的五个不同的位置值

    一.position: static  无定位 HTML 元素默认情况下的定位方式为 static(静态). 静态定位的元素不受 top.bottom.left 和 right 属性的影响. posi ...

  8. Hyper-V CPU设置

    前言 最近在用Hyper-V测试项目,发现在运行过程中发现项目总数崩掉,几经发现有一个共性,CPU占用率100%,分析问题发现问题出在Hyper-V CPU设置上,Hyper-V装系统就不赘述了,网上 ...

  9. B站视频:CocosCreator Bundle 特性三个实例详解,轻松实现大厅子游戏模式

    详细内容:https://forum.cocos.org/t/topic/112146

  10. Linux安装配置Java

    先从 Oracle 官网下载 Java 运行 tar -zxvf xxxx.tar.gz 指令将 Java 解压到 /usr/local/java 下(个人习惯,无所谓) 修改环境变量 vim /et ...