一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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

Follow up:

Can you solve it without using extra space?

(二)解题

本题大意:给定一个链表,判断链表里面是否成环。不能用辅助空间。

解题思路:利用快慢指针,如果有环的话,慢指针总会碰到快指针。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode* p1 = head;//慢指针
        ListNode* p2 = head;//快指针
        while(p1!=NULL&&p2!=NULL&&p2->next!=NULL)//如果没有环,总会指向NULL
        {
            p1=p1->next;
            p2=p2->next->next;
            if(p1==p2) return true;//慢指针追上快指针,表示成环
        }
        return false;
    }
};

【一天一道LeetCode】#141. Linked List Cycle的更多相关文章

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

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

  2. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

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

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

  6. leetcode 141 Linked List Cycle Hash fast and slow pointer

    Problem describe:https://leetcode.com/problems/linked-list-cycle/ Given a linked list, determine if ...

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

  8. Java for 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 ex ...

  9. leetcode 141. Linked List Cycle ----- java

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  10. Python [Leetcode 141]Linked List Cycle

    题目描述: Given a linked list, determine if it has a cycle in it. 解题思路: 快的指针和慢的指针 代码如下: # Definition for ...

随机推荐

  1. Java Servlet 笔记2

    1. Servlet的生命周期 Servlet 通过调用 init () 方法进行初始化. Servlet 调用 service() 方法来处理客户端的请求. Servlet 通过调用 destroy ...

  2. c++读写MySQL

    看过很多C或是C++操作MySQL数据库的文章,大部分太吃力了,甚至有一部分根本没有很好的组织文字,初学者比较难以接受,即使是C++或是C高手也是比较难看懂.写这篇文章的目的不是别的,就一个,告诉您用 ...

  3. candy(动态规划)

    题目描述 There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  4. C语言程序设计第二次作业——顺序结构

    (一)改错题 1.输出带框文字:在屏幕上输出以下3行信息. 错误信息1: 错误原因:i和d位置错误 改正方法:i和d位置互换 错误信息2: 错误原因:\n后缺了一个" 改正方法:\n后加一个 ...

  5. 第一次C语言作业

    1. 求圆的面积和周长 输入圆的半径,求圆的周长和面积 流程图 测试结果: 实验问题:1.加号输入到引号内部导致运算终止 解决办法:通过改正加号位置是算法正确并继续运行 2判断闰年 输入一个四位年份, ...

  6. 网络不能上网但能ping通处理

    同事电脑不能上网,经过检查可以排除网线和网络问题,默认网关以及网页地址都能ping通,从网上搜索说是LSP问题,但是根据其操作方式修复,还是无效. 最后,不知道怎么捣鼓的,问题解决了. 操作流程: 1 ...

  7. Answers to "Why are my jobs not running?"

    from :https://community.oracle.com/thread/648581 This is one of the most common Scheduler questions ...

  8. POJ 放苹果问题(递归)

    首先我们想象有一个函数count  f(m,n)可以把m个苹果放到n个盘子中. 根据 n 和 m 的关系可以进一步分析: 特殊的m <=1|| n <= 1时只有一种方法: 当 m < ...

  9. ACM | HDU|6227_Rabbit

          题意:     有n只兔子分别占据不同的位置,任意一只兔子可以插入任意两只兔子的之间,但要求两只兔子之间要有空位,求这样的移动次数最多能够有多少?   在这里每一只兔子没有区别,可以看做把 ...

  10. Docker内核能力机制

    能力机制(Capability)是 Linux 内核一个强大的特性,可以提供细粒度的权限访问控制. Linux 内核自 2.2 版本起就支持能力机制,它将权限划分为更加细粒度的操作能力,既可以作用在进 ...