一天一道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. 【iOS】苹果开发者账号申请

    [1]首先登陆苹果开发者中心:https://developer.apple.com/programs/ 如图有一个按钮enroll,意思是苹果开发者报名(说白了就是要交钱,好让你具备APP测试和上线 ...

  2. js中json字符串与json对象的相互转换

    web前端开发过程中,数据传输json是以字符串的形式传递,而js操作的是JSON对象. 一.JSON字符串转换为JSON对象 var obj = JSON.parse(str[, reviver]) ...

  3. Vue.js + Webpack

    vue.js Vue.js是一个构建数据驱动的 web 界面的库.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件 以上是Vue.js官方定义,故名思议,以数据驱动视 ...

  4. SpringMVC mock测试详解

    @RunWith(SpringRunner.class) @SpringBootTest(classes = WebmanagerApplication.class) //配置事务的回滚,对数据库的增 ...

  5. SpringMVC注解@Component、@Repository、@Service、@Controller区别

    SpringMVC中四个基本注解: @Component.@Repository   @Service.@Controller 看字面含义,很容易却别出其中三个: @Controller   控制层, ...

  6. python学习之路网络编程篇(第三篇)

    python线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/usr/bin/env python # -*- coding:utf-8 -*- import t ...

  7. ACM Find them, Catch them

    The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TW ...

  8. ACM Bee

    In Africa there is a very special species of bee. Every year, the female bees of such species give b ...

  9. Node.js 集群

    稳定性: 2 - 不稳定 单个 Node 实例运行在一个线程中.为了更好的利用多核系统的能力,可以启动 Node 集群来处理负载. 在集群模块里很容易就能创建一个共享所有服务器接口的进程. var c ...

  10. 论文答辩ppt要怎么写

    1.总体原则: 字大.字少.图多.要有重点 字体建议:正文要用黑体(如微软雅黑),标题可使用宋体或者黑体(如微软雅黑) 2. 主要分为两大部分: 2.1系统介绍 系统概述:概述自己系统主要是做了些什么 ...