题目

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

Follow up:

Can you solve it without using extra space?

分析

判断链表是否有环,采用快慢指针,如果相遇则表示有环

AC代码

/**
* 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) {
if(!head || !head->next){
return false;
}
ListNode* slow = head;
ListNode* fast = head->next;
while(fast->next && fast->next->next && fast != slow){
fast = fast->next->next;
slow = slow->next;
}
return fast == slow;
}
};

leetCode-linkedListCycle判断链表是否有环的更多相关文章

  1. LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

  2. LeetCode 141. Linked List Cycle(判断链表是否有环)

    题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...

  3. [Leetcode] 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. Follow up:Can you solve it without using ext ...

  5. [Leetcode] Linked list cycle ii 判断链表是否有环

    Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...

  6. 141 Linked List Cycle(判断链表是否有环Medium)

    题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...

  7. 判断链表是否有环(Java实现)

    判断给定的链表中是否有环.如果有环则返回true,否则返回false. 解题思路:设置两个指针,slow和fast,fast每次走两步,slow每次走一步,如果有环的话fast一定会追上slow,判断 ...

  8. 141. Linked List Cycle(判断链表是否有环)

    141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...

  9. python判断链表是否有环

    思路:使用快慢指针,快指针每次走两步,慢指针每次走一步,如果有环,则一定会快慢指针指向同一结点: 假设环的长度为n,先让一个指针走n步,另一个再开始走,当他们指针指向同一结点时,该结点就是环入口点 ( ...

随机推荐

  1. multiDex分包时指定主dex的class列表

    在gradle中我们使用了如下代码可以将指定类型分配到主dex中: afterEvaluate { tasks.matching { it.name.startsWith('dex') }.each ...

  2. shiro入门实例,基于ini配置

    基于ini或者关系数据库的,其实都是一样的,重要的是思想. # ==================================================================== ...

  3. 机器学习【工具】:Numpy基础

    Numpy Numpy 是 Python 数据科学计算的核心库,提供了高性能的多维数组对象及处理数组的工具 使用方式 import numpy as np 数组 生成数组  简单生成 a = np.a ...

  4. js字符串方法汇总

    1.length方法 var stringObject=new String("hellow world"); console.log(stringObject.length);/ ...

  5. swift 字体自适应,宽高自适应

    let kScreenWidth = UIScreen.main.bounds.width let kScreenHeight = UIScreen.main.bounds.height public ...

  6. CentOS SSH免密登陆

    #环境说明客户机:Mac OS X服务器:CentOS 6.5客户端:OpenSSH,OS X及大多数Linux都内置了OpenSSH.’ssh -v’命令可以查看版本. #大致流程1.在客户机创建一 ...

  7. 笔记:mysql升序排列asc,降序排列desc

    经常会忘记mysql中升序和降序用什么字符来表示,现在就做个笔记:升序排列asc,降序排列desc,举个例子,下面是按时间降序调用栏目的文章,也即是栏目最新文章 [e:loop={"sele ...

  8. host文件

    # Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP f ...

  9. what's the 头寸

    头寸,是一种市场约定,承诺买卖外汇合约的最初部位,买进外汇合约者是多头,处于盼涨部位:卖出外汇合约为空头,处于盼跌部位.头寸可指投资者拥有或借用的资金数量. “头寸”一词来源于近代中国,银行里用于日常 ...

  10. what's the 爬虫之基本原理

    what's the 爬虫? 了解爬虫之前,我们首先要知道什么是互联网 1.什么是互联网? 互联网是由网络设备(网线,路由器,交换机,防火墙等等)和一台台计算机连接而成,总体上像一张网一样. 2.互联 ...