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

分析:快慢指针。

/**
* 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. * ./common/http.js in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-opt

    vue项目报错如下,找到原因之后,其实超简单,请看: 原来是引入文件路径出现问题,想起刚刚引入了一个文件,一修改,果然药到病除! ----------------------------------- ...

  2. (爬虫)随机生成一个header

    #!/usr/bin/env python #-*- coding: utf-8 -*- #__Author__: yunrui #__Version__: 1.0 #__Time__: 2019/1 ...

  3. C++:打开一个文件夹下一系列的文件

    可以用MFC的CFileFind类: FILE *pFile=NULL; CFileFind cff; CString fstr="C:\\page\\*.*"//所以用文件和文件 ...

  4. Vue - 如何使用npm run build后的dist文件夹

    脚手架vue cli生成项目后,使用 npm run build 生成了一个dist文件夹(应该是distribution的缩写) 只要放在http服务器上就可以运行. 使用一句python命令可以搭 ...

  5. php自动读取文件夹下所有图片

    $path = 'xxxxx';///当前目录$handle = opendir($path); //当前目录while (false !== ($file = readdir($handle))) ...

  6. 支付接口API

    //微信支付SDK https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

  7. C语言-switch语句的使用。对文件的输出处理。for循环和if的结合使用。

    //函数fun功能:统计字符串中各元音字母的个数,注意:不区分大小写. //重难点:switch语句的使用. #include <stdlib.h> #include <conio. ...

  8. php 算false的情况

    四.PHP中算false的情况 1.Boolan false 2.整形 0 3.浮点型 0.0 4.字符串"" "0" ("0.0" &qu ...

  9. 微服务、分库分表、分布式事务管理、APM链路跟踪性能分析演示项目

    好多年没发博,最近有时间整理些东西,分享给大家. 所有内容都在github项目liuzhibin-cn/my-demo中,基于SpringBoot,演示Dubbo微服务 + Mycat, Shardi ...

  10. put、patch与post区别

    idempotent 幂等的 如果一个方法重复执行多次,产生的效果是一样的,那就是idempotent的:  idempotent的意思是如果相同的操作再執行第二遍第三遍,結果還是一樣. POST方法 ...