Linked List Cycle 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/linked-list-cycle/description/


Description

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

Follow up:

Can you solve it without using extra space?

Solution

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

解题描述

这道题是要检查链表是否有环。通过设立两个游标,都从head开始,一个每次往后移动一个节点,一个每次往后移动两个节点。如果两个游标相遇则说明有环。这个问题借用网上的说法就是,在操场上有两个人在跑步,一个人速度是另一个人的两倍,那这两个人肯定会相遇。

[Leetcode Week6]Linked List Cycle的更多相关文章

  1. [Leetcode Week6]Linked List Cycle II

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

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

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

  9. 【题解】【链表】【Leetcode】Linked List Cycle II

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

随机推荐

  1. C++学习009预处理器指令符号 # ## #@ 符号的使用

    # ## #@ 符号是预处理器指令符号. 当预处理器遇到#指令符号时,会将#之后的部分用双引号括起来 当预处理去遇到##指令符号时,直接将##前后部分连接起来 当预处理器遇到#@指令符号,将#@之后的 ...

  2. SQL 注入教程

    SQL 注入测评教程 1     准备 安装包:Burpsuit.Python27.sqlmap 2     安装配置 2.1    Burpsuit 1)       解压Burpsuit 2)   ...

  3. django视图之分页

    在网站开发时,肯定会遇到分页的事情需要处理,在django中也是如此,在Django中处理分页一般会使用到两个类django.core.paginator.Paginator和django.core. ...

  4. BZOJ 1565 NOI2009 植物大战僵尸 topo+最小割(最大权闭合子图)

    题目链接:https://www.luogu.org/problemnew/show/P2805(bzoj那个实在是有点小小的辣眼睛...我就把洛谷的丢出来吧...) 题意概述:给出一张有向图,这张有 ...

  5. HDU 4569 Special equations(枚举+数论)(2013 ACM-ICPC长沙赛区全国邀请赛)

    Problem Description Let f(x) = anxn +...+ a1x +a0, in which ai (0 <= i <= n) are all known int ...

  6. 软件工程项目组Z.XML会议记录 2013/09/25

    软件工程项目组Z.XML会议记录 [例会时间]2013年9月25日周三21:30-22:10 [例会形式]小组讨论 [例会地点]三号公寓楼会客厅 [例会主持]李孟 [会议记录]肖俊鹏 会议整体流程 签 ...

  7. Python模块学习:logging 日志记录

    原文出处: DarkBull    许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net ...

  8. 深度优先搜索(DFS),逃离迷宫

    [原创] 今天来说说深度优先搜索,深度优先是图论中的内容,大意是从某一点出发,沿着一个方向搜索下去,并伴随着有回退的特点,通常用来判断某一解是否存在,不用来寻找最优解:这里来看一个非常有意思的题目: ...

  9. OO5-7次作业总结

    写在最前面: 转眼间就又到了一月一次的总结时间,这次的三个作业,我个人感觉可能是最令人难受的三次作业了.不只是因为它们是多线程,更是因为它们几乎是全新的三次作业,每次的代码几乎都要重头开始. 第五次作 ...

  10. [剑指Offer] 40.数组中只出现一次的数

    题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. [思路]此题考察的是异或运算的特点:即两个相同的数异或结果为0. 此题用了两次异或运算特点: ( ...