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. 第三十篇 面向对象的三大特性之继承 supre()

    继承 一 .什么是继承? 类的继承跟现实生活中的父.子.孙子.重孙子的继承关系一样,父类又称基类. Python中类的继承分为:单继承 和  多继承. # 定义父类 class ParentClass ...

  2. javaX邮件发送

    /** * *  * @param mailServerHost 邮件服务器 * @param mailServerPort 端口 * @param validate 是否需要身份验证 * @para ...

  3. 【iOS开发】创建单例的两种方法

    创建一个单例很多办法.我先列举一个苹果官方文档中的写法. [cpp] view plaincopy   static AccountManager *DefaultManager = nil; + ( ...

  4. 最短路径——Bellman-Ford算法以及SPFA算法

    说完dijkstra算法,有提到过朴素dij算法无法处理负权边的情况,这里就需要用到Bellman-Ford算法,抛弃贪心的想法,牺牲时间的基础上,换取负权有向图的处理正确. 单源最短路径 Bellm ...

  5. lintcode-59-最接近的三数之和

    59-最接近的三数之和 给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和. 注意事项 只需要返回三元组之和,无需返回三元组本身 样例 例如 S = ...

  6. web相关基础知识2

    2017-12-14 17:14:22 块元素 典型代表,Div,h1-h6,p,ul,li 特点: ★独占一行 ★可以设置宽高  ★ 嵌套(包含)下,子块元素宽度(没有定义情况下)和父块元素宽度默认 ...

  7. PAT 甲级 1002 A+B for Polynomials

    https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000 This time, you are sup ...

  8. 虚拟机CentOS7.2 1611 Minimal最小化安装后桥接固定ip

    ip addr show 或者 ip addr 或者 ip a vim /etc/sysconfig/network-scripts/ifcfg-ens33 根据 然后重启网卡 service net ...

  9. javascript中将整数添加千位符号

    如果num是整数的话,将其转换成带千位符号的字符串: Number(num).toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' +  ','); 另 ...

  10. DataGridView过滤功能

    http://www.codeproject.com/Articles/33786/DataGridView-Filter-Popup http://www.cnblogs.com/jaxu/arch ...