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. JMeter学习笔记(九) 参数化4--User Variables

    4.User Variables 用户参数 1)线程组右键添加 -> 前置处理器 -> 用户参数 2)配置用户参数 3)添加HTTP请求,引用用户参数,格式: ${} 4)配置线程数 5) ...

  2. 从底层带你理解Python中的一些内部机制

    下面博文将带你创建一个字节码级别的追踪API以追踪Python的一些内部机制,比如类似YIELDVALUE.YIELDFROM操作码的实现,推式构造列表(List Comprehensions).生成 ...

  3. Visual Studio 6.0安装包

    点击下载

  4. 安装floodlight遇到的问题和解决

    环境:ubuntu18.04 安装floodlight先前准备:java的环境,ant. sudo apt-get install build-essential defailt-jdk ant py ...

  5. ASP.NET CORE 2.0 文档中文正式版已经出来了

    https://docs.microsoft.com/zh-cn/aspnet/core/

  6. hadoop worldcount小程序

    首先在hadoop中建立input文件夹放几个文件,里边写点东西.比如我放了三个,分别写的是 第一个 hello hadoop bye hadoop 第二个 hello world bye world ...

  7. 数组中键key相等时,后面的值覆盖前面的值

    <?php $arr[]='abc'; $arr[]='; $arr[]='; $arr[]='; var_dump($arr); 结果;

  8. yii视频地址哦

    https://www.yiichina.com/video  

  9. maven第一个HelloWorld

    1.Maven约定好的目录结构: maven01/src --main --java --package //包有多少级,加多少个子folder --新建一个HelloWorld.java --tes ...

  10. 【bzoj5060】魔方国 乱搞+特判

    题目描述 一张未知的有重边无自环的图,只知道点数为n,边数为m.可以标记若干个点,如果一个点被标记,那么与它距离不超过k的点(包括本身)都会被覆盖. 显然对于每张不同图,让所有点被覆盖的最小代价是不一 ...