题意:判断链表是否有环。

分析:快慢指针。

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

  

LeetCode 141. Linked List Cycle(判断链表是否有环)的更多相关文章

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

  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. 141. Linked List Cycle(判断链表是否有环)

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

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

  5. 141 Linked List Cycle(判断链表是否有环Medium)

    题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...

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

  7. LeetCode 141. Linked List Cycle环形链表 (C++)

    题目: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked ...

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

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

随机推荐

  1. MYSQL内置函数【转】

    一.MySQL 字符串函数 函数 描述 实例 ASCII(s) 返回字符串 s 的第一个字符的 ASCII 码. 返回 CustomerName 字段第一个字母的 ASCII 码: SELECT AS ...

  2. 3、示例(在java中使用JSON)

    教程链接(json-smple1.1.1.jar文件) 链接:http://pan.baidu.com/s/1qXPbYHm 密码:v0f0 如何使用java编程语言编码和解码JSON 首先准备环境以 ...

  3. iframe多层嵌套时,Jquery获取元素

    在项目中,尤其是后台管理项目,会使用到iframe嵌套的网页,说起iframe,真的是个让人头疼的东西,能避开是最好避开.不然要请随身备好氧气瓶哈(因为管理和调试过程中往往会被气缺氧!!!哈哈哈~~~ ...

  4. cas的单点登录实现

    1.  前提条件 环境:jdk1.8.shiro1.4.0及以上版本.项目以 spring+shiro构建 工具:buji-pac4j-3.1.0-jar-with-dependencies.jar以 ...

  5. IDEA启动报错-java.net.BindException: Address already in use: bind

    启动IDEA报错日志如下: Internal error. Please refer to http://jb.gg/ide/critical-startup-errors java.net.Bind ...

  6. 00-Docker基本安装

    目录 00-Docker基本安装 参考 安装与配置 启动与测试 00-Docker基本安装 Docker Version: 19.03.5

  7. RedisTemplate 获取过期时间的问题

    RedisTemplate redisTemplate; Long seconds = redisTemplate.opsForValue().getOperations().getExpire(&q ...

  8. LLC半桥谐振变换器调试记录

    1.判断二极管是否击穿 2.判断mos管是否烧坏 直接用声音档,发出响声说明击穿了 3.测试二极管的正负极方法 将万用表调到二极管档 1.信号发生芯片周围的电阻 2.反馈部分的电阻 3.实验准备部分: ...

  9. java 协程

    协程是比线程更轻量级的程序处理单元,也可以说是运行在线程上的线程,由自己控制 1.适用于被阻塞的,且需要大量并发的场景. 2.不适用于,大量计算的多线程,遇到此种情况,更好实用线程去解决. 虽然Jav ...

  10. 《如何写好商业计划书》---创业学习---训练营第三课---HHR---

    一,<开始上课> 1,投资人不愿意约见的原因:创始人没有把项目的投资价值和亮点呈现在商业计划书里. 2,BP的三个常见的错误:不够完整,关键内容没有呈现出来:华而不实:篇幅过长. 3,预热 ...