https://oj.leetcode.com/problems/linked-list-cycle/

判断一个链表是否为循环链表(这个链表可能是 1 2 3 4 然后4指向2)

巧妙的方法:设置两个指针,一个slow,一个fast。每次slow走一个,fast走两个,如果是循环链表,它俩有相等的时候。

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

LeetCode OJ--Linked List Cycle **的更多相关文章

  1. [LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  2. 【Leetcode】Linked List Cycle II

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

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

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

  5. Java for LeetCode 142 Linked List Cycle II

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

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

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

  7. [Leetcode Week6]Linked List Cycle II

    Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...

  8. [Leetcode Week6]Linked List Cycle

    Linked List Cycle 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/linked-list-cycle/description/ Des ...

  9. [LeetCode] 142. Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...

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

随机推荐

  1. SSRS 报表管理器 http://localhost/Reports HTTP500 内部错误处理过程

    原文地址:http://www.cnblogs.com/zzry/p/5716056.html 安装了很多机器的sqlserverBI 组件 初始安装配置下 浏览报表管理器 http://localh ...

  2. rest_framework序列化

    1.序列化 1)拿到queryset 2)将queryset 给序列化类 serializer = IdcSerializer(idc)    #单个对象 serializer = IdcSerial ...

  3. laravel5.2总结--关联关系

     参考文章 http://laravelacademy.org/post/1095.html http://laravelacademy.org/post/1174.html http://d.lar ...

  4. laravel5.2总结--门面(facades)

    Facades 为应用程序的服务容器中可用的类提供了一个「静态」接口.   Laravel 本身附带许多的 facades,甚至你可能在不知情的状况下已经在使用他们!   xpower的静态接口(门面 ...

  5. JS空数组的判断

    前言 最近在做一个mini项目,被大神各种鄙视,基础知识确实是不扎实,加油加油.好了,不多废话,抽空写写遇到的两个知识点,就记录下来,写博客还是能帮忙整理记录的,不然过了就忘记了. input监听值改 ...

  6. [转]Jupyter NoteBook 的快捷键使用指南

  7. ALPHA 冲刺(一)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内 ...

  8. 观15级K班团队作业有感

    1.指尖加密 特点:通过可移动设备手机参与电脑文件的解密,使加密更加安全. 缺点:跟柯逍老师的想法差不多,UI简陋,操作不是很友好,或许可以加一个帮助文档. 2.youreyes 特点:可以检测路过的 ...

  9. MySql数据库 - 3.利用MySql Workbench 对数据库进行操作

    打开MySql Workbench 选择呢一个数据库 查看数据库: 创建数据库 在SCHEMAS下的空白位置右键 - 选择 Create Schema... 如果数据库名字中有大写字母,会出现如下提示 ...

  10. cf 853 B Jury Meeting [前缀和]

    题面: 传送门 思路: 看完题目以后,首先有一个结论:每个人都是先去到首都,等待开会,开会结束以后再一个个走掉 而且这道题只有去首都和离开首都的机场 因此考虑计算去首都的飞机的前缀最小花费,以及离开首 ...