非常简单的题:判断链表有没有环(用快慢指针)

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

【easy】141. Linked List Cycle的更多相关文章

  1. 【LeetCode】141. Linked List Cycle (2 solutions)

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  2. 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...

  3. 【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 ...

  4. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  5. 【LeetCode】142. Linked List Cycle II (2 solutions)

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  6. 【一天一道LeetCode】#141. Linked List Cycle

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. 【leetcode❤python】141. Linked List Cycle

    #-*- coding: UTF-8 -*- #Method:快慢指针法,建立虚表头,快指针走两步,慢指针走一步,若存在环,则快指针会追上慢指针# Definition for singly-link ...

  8. 【Lintcode】103.Linked List Cycle II

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

  9. 【Lintcode】102.Linked List Cycle

    题目: Given a linked list, determine if it has a cycle in it. Example Given -21->10->4->5, ta ...

随机推荐

  1. Python学习步骤如何安排?

    一.清楚学习目标 无论是学习什么知识,都要有一个对学习目标的清楚认识. 只有这样才能朝着目标持续前进,少走弯路,从学习中得到不断的提升,享受python学习计划的过程. 二.基本python 知识学习 ...

  2. 证明与计算(3): 二分决策图(Binary Decision Diagram, BDD)

    0x01 布尔代数(Boolean algebra) 大名鼎鼎鼎的stephen wolfram在2015年的时候写了一篇介绍George Boole的文章:George Boole: A 200-Y ...

  3. 微信网页分享 jssdk config:invalid signature 签名错误

    invalid signature签名错误.建议按如下顺序检查: 确认签名算法正确,可用 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisi ...

  4. HTML事件属性

    1.常用窗口事件属性 属性 值 描述 onbeforeonload script 在文档加载之前运行脚本 onblur script 当窗口失去焦点时运行脚本 onerror script 当错误发生 ...

  5. beego框架开发投票网站(1) beego基础之运行逻辑

    本文档需结合beego官方文档食用 博主也仅仅是边学边记录,不保证内容的正确性,请当做通俗读物来看待 首先 beego是一个基于go语言的框架 其次 beego是一个mvc框架 框架可以理解为对底层又 ...

  6. Linux系统中硬链接和软链接(符号链接)的区别

    首先是创建链接的命令 ln file link //创建硬链接 ln -s item link //创建软链接 区别 硬链接 硬链接是一开始Unix创造链接的方式,而软连接就更现代一点.创建硬链接的时 ...

  7. Logstash处理json格式日志文件的三种方法

    假设日志文件中的每一行记录格式为json的,如: {"Method":"JSAPI.JSTicket","Message":"JS ...

  8. virtualbox+ubuntu

    https://jingyan.baidu.com/article/7f766daff541cd4101e1d0cd.html ubuntu 安装 这台计算机似乎没有安装操作系统 待解决 注意ubun ...

  9. Java基础--面向对象编程2(封装)

    1.封装的定义: 封装:将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问. 2.  为什么需要封装?封装的作用和含义? 首先思考一个问题:当我们要 ...

  10. 前端安全类——CSRF/XSS

    CSRF 概念:跨站请求伪造 全称:Cross-site request forgery 攻击原理:网站中某一个接口存在漏洞,用户在注册网站登录过 防御措施: 1.Token验证:引诱链接只会自动携带 ...