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. 获取发布版SHA1和调试版SHA1

    总结 调试版: 常见问题 | 高德地图API (amap.com) 发布版: 首先需要生成签名 Android Studio生成签名文件,自动签名,以及获取SHA1和MD5值_donkor_的博客-C ...

  2. Windows难民安装docker的注意事项

    Windows下如何安装docker,这个没啥可说的,一直下一步就ok Windows  docker 下载地址: https://download.docker.com/win/stable/Doc ...

  3. ecshop调用指定广告的方法

    在include/lib_goods.php文件下面新增:function getads($cat,$num){ $time = gmtime();$sql = "SELECT * FROM ...

  4. Matlab使用随记

    Matlab 2020 想要看图像每一点的值大小 工具--->数据提示 想要导出的分辨率提高 导出设置--->渲染--->600dpi Matlab 2017b 程序运行后,画出图, ...

  5. hadoop报错

    19/11/24 08:29:08 INFO qlh.MyMapreduce: ================this is job================= 19/11/24 08:29: ...

  6. Hive On Spark保姆级攻略

    声明: 此博客参考了官网的配置方式,并结合笔者在实践网上部分帖子时的踩坑经历整理而成 这里贴上官方配置说明: [官方]: https://cwiki.apache.org//confluence/di ...

  7. P2179-[NOI2012]骑行川藏【导数,二分】

    正题 题目链接:https://www.luogu.com.cn/problem/P2179 题目大意 给出\(E\)和\(n\)个\(s_i,k_i,u_i\)求一个序列\(v_i\)满足 \[\s ...

  8. Kettle学习笔记(一)— 环境部署及运行

    目录 Kettle学习笔记(一)-环境部署及运行 Kettle学习笔记(二)- 基本操作 kettle学习笔记(三)- 定时任务的脚本执行 Kettle学习笔记(四)- 总结 Kettle简介 Ket ...

  9. 【Go】Golang实现gRPC的Proxy的原理

    背景 gRPC是Google开始的一个RPC服务框架, 是英文全名为Google Remote Procedure Call的简称. 广泛的应用在有RPC场景的业务系统中,一些架构中将gRPC请求都经 ...

  10. Windows10通过WSL编译jdk12

    Windows使用WSL编译OpenJDK 安装Ubuntu以及配置国内镜像 首选确保windows10已经安装了ubuntu 更换ubuntu20.04国内镜像,这里我选择的是阿里云镜像 sudo ...