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

/**
* 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. mysql联合主键,也就是两个数据字段一起做主键的情况

    一个数据表,需要两个字段联合起来一块做主键的时候.举例如下: 直接用sql语句的话如下 ALTER TABLE `表名` ADD PRIMARY KEY ( `表中字段名1` , `表中字段名2` ) ...

  2. 重写Sink合并多行

    flume1.6+elasticsearch6.3.2 Pom <dependencies> <dependency> <groupId>junit</gro ...

  3. Python基础:数据类型-字符串(7)

    1.字符串基本操作 字符串是由字符组成的一串字符序列,字符串是有顺序的,从左到右,索引从0开始,依次递增. Python中字符串类型:str. Python中字符串的三种表示方式: (1)普通字符串: ...

  4. -bash: fork: Cannot allocate memory 问题的处理

    今天生产机器突然无法登录了,正好有一个用top挂着,但是退出top,执行任何命令都报-bash: fork: Cannot allocate memory,但是查看内存还是有很多空闲,然后在百度上查了 ...

  5. BZOJ3033太鼓达人——哈密顿回路/欧拉回路

    题目描述 七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行.这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是刚刚被精英队伍成员XLk.Poet_shy和lydrainbo ...

  6. PHP——??空合并运算符和?:三元运算符

    前言 在上一篇随笔,用三元运算符简单写的一个东西,引发了对他的兴趣,所以打算研究下. PHP7的新特性: https://php.net/manual/zh/migration70.new-featu ...

  7. cmd 常用命令

    注:绿色的为比较常用的命令 命令 名称 ASSOC  显示或修改文件扩展名关联. ATTRIB 显示或更改文件属性. BREAK  设置或清除扩展式 CTRL+C 检查. CACLS 显示或修改文件的 ...

  8. settings 配置 + 测试环境搭建

    若想将模型转为mysql数据库中的表,需要在settings中配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', ...

  9. CF1157A-Reachable Numbers题解

    原题地址 题目大意:有一个函数\(f(x)\),效果是将\(x+1\)后,去掉末尾所有的\(0\),例如: \(f(599)=6\),因为\(599+1=600→60→6\) \(f(7)=8\),因 ...

  10. 小白月赛13 小A的路径 (矩阵快速幂求距离为k的路径数)

    链接:https://ac.nowcoder.com/acm/contest/549/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...