Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

思路:采用“快慢指针”查检查链表是否含有环。让一个指针一次走一步,另一个一次走两步,如果链表中含有环,快的指针会再次和慢的指针相遇。

class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* slow = head;
ListNode* fast = head; while(fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
return true;
}
return false;
}
};

141. Linked List Cycle (List; Two-Pointers)的更多相关文章

  1. 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)

    题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...

  2. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

  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. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  5. 141. Linked List Cycle【easy】

    141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

  6. 141. Linked List Cycle - LeetCode

    Question 141. Linked List Cycle Solution 题目大意:给一个链表,判断是否存在循环,最好不要使用额外空间 思路:定义一个假节点fakeNext,遍历这个链表,判断 ...

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

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

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

  10. 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. cnpm 私服搭建(基于docker)

    备注:   使用docker-compose 进行安装 1. 代码clone git clone https://github.com/cnpm/cnpmjs.org.git 2. docker bu ...

  2. 系列文章--一步一步学Silverlight2

    概述 由TerryLee编写的<Silverlight 2完美征程>一书,已经上市,在该系列文章的基础上补充了大量的内容,敬请关注.官方网站:http://www.dotneteye.cn ...

  3. pandas之DateFrame 数据过滤+遍历行+读写csv-txt-excel

    # XLS转CSV df = pd.read_excel(r'列表.xls') df2 = pd.DataFrame()df2 = df2.append(list(df['列名']), ignore_ ...

  4. ssdb的golang驱动的同步问题

    如果数据库连接只有一个,在某个时间点(指的是某个及其短的时间内),多个读写的话,会出问题,修改了下,加了个mutex,算是解决了此问题,贴下备忘 var mutex sync.Mutex func ( ...

  5. coding style 的两点

    通俗介绍coding style两点建议: 模块划分 这个如果做得不好,简直不能忍.有的代码非常莫名其妙,有些东西本身不复杂,非要将其拆成好几个部分,然后做成一个一个碎散的模块,这样并不好.举个例子, ...

  6. MySQL 加快导入数据

    1.临时关闭binlog,避免写入日志 set sql_log_bin = off: mysql> show VARIABLES like '%log_bin%'; +------------- ...

  7. Linux学习笔记 - Shell 控制语句

    if 语句 语法: #!/bin/bash a= b= if [ $a -eq $b ] then echo "a 等于 b" elif [ $a -gt $b ] then ec ...

  8. PyQt 5事件和信号

    信号槽Signals & slots sld.valueChanged.connect(lcd.display) # 将滚动条的valueChanged信号连接到lcd的display插槽 # ...

  9. jQuery笔记——插件

    验证插件 验证插件(validate.js),是一款验证常规表单数据合法性的插件.使用它,极大的解 放了在表单上繁杂的验证过程,并且错误提示显示的完善也增加了用户体验 插件都可以在JQueryUI 的 ...

  10. 【UVa】1343 The Rotation Game(IDA*)

    题目 题目     分析 lrj代码.... 还有is_final是保留字,害的我CE了好几发.     代码 #include <cstdio> #include <algorit ...