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. Vm-Ubuntu下配置Qt开发环境

    在昨天的Ubuntu换降下,安装Qt发现编译的时候是缺少opengl的 奈何找了好多方式都无法安装opengl 今天看到另一位大神写的,才发下自己找的还是有问题 大神帖子网址:http://blog. ...

  2. 5、shader混合(Blending)、雾

    直接上效果图:queue:transparent 雾: fog { mode exp color(0.6,0.4,0.3,0.8) density 0.3 range ,0.8 } 效果: http: ...

  3. PostgreSQL基本配置

    记一下Postgresql的基本操作,在Ubuntu下使用apt-get安装是不会像MySQL那样都配置好了,而是要安装后再配置: 1. 基本安装 # 安装postgresql和pgadmin(一个管 ...

  4. Word2Vec词向量(一)

    一.词向量基础(一)来源背景  word2vec是google在2013年推出的一个NLP工具,它的特点是将所有的词向量化,这样词与词之间就可以定量的去度量他们之间的关系,挖掘词之间的联系.虽然源码是 ...

  5. flask - 1

    from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, Worl ...

  6. C++ 中神奇的头文件,懒人专用

    今天在做题的时候,偶然发现了一种神奇头文件.他的使用方法以及内容如下: #include <bits/stdc++.h> // C++ includes used for precompi ...

  7. 解决IIS的Server Application Error

    问题描述一下: Server Application ErrorThe server has encountered an error while loading an application dur ...

  8. HihoCoder 1480:矩阵填数 (杨氏矩阵 || 钩子公式 + 筛逆元)

    描述 小Hi在玩一个游戏,他需要把1, 2, 3, ... NM填入一个N行M列的矩阵中,使得矩阵每一行从左到右.每一列从上到下都是递增的. 例如如下是3x3的一种填法: 136 247 589 给定 ...

  9. Android逆向之旅---爆破一款资讯类应用「最右」防抓包策略原理分析

    一.逆向分析 首先感谢王同学提供的样本,因为王同学那天找到我咨询我说有一个应用Fiddler抓包失败,其实对于这类问题,我一般都会这么回答:第一你是否安装Fiddler证书了,他说他安装了.第二你是否 ...

  10. 【模拟赛·polyline】

    Input file: polyline.in Output file: polyline.out Time limit: 1s Memory limit: 128M 有若⼲个类似于下⾯的函数: 定义 ...