141. Linked List Cycle

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

Follow up:
Can you solve it without using extra space?

利用快慢指针,如果相遇则证明有环

注意边界条件: 如果只有一个node.

 public class Solution {
public boolean hasCycle(ListNode head) {
if(head==null || head.next==null) return false;
ListNode slower =head,faster = head;
while(faster!=null && faster.next!=null){
if(faster==slower) return true;
faster = faster.next.next;
slower = slower.next;
}
return false;
}
}
 

141. Linked List Cycle(判断链表是否有环)的更多相关文章

  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]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. 141 Linked List Cycle(判断链表是否有环Medium)

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

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

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

  7. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

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

  9. LeetCode 141. Linked List Cycle环形链表 (C++)

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

随机推荐

  1. linux下安装pip以及导入第三方包

    python有着强大的第三方库,数量很多且功能强大. 最原始的办法是在官网上下载压缩包,解压,然后运行setup.py来进行安装. 显然这种方法很繁琐,不方便.因此有了包管理工具. pip是一个包管理 ...

  2. 最近maven开发中遇到的一些bug。

    1.WebxContextLoaderListener  等tomcat启动报错.大部分原因都是jar包问题. 检查方式,在tomcat的webapps/WEB-INF/lib下有没有想对应的jar包 ...

  3. java集合的中的集合关系实现或继承关系图

    放在这儿一目了然.

  4. linux--GCC简单用法

    gcc是linux下最常用的一款c编译器,对应于CPP 有相应的g++工具,debug有gdb,只是还不会用. 个人感觉gcc确实是个好东西,完全可以直接在gedit下编程然后写个shell脚本用gc ...

  5. js正则函数match、exec、test、search、replace、split使用介绍集合,学习正则表达式的朋友可以参考下。

    match 方法 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) 参数 stringObj 必选项.对其进行查找的 String 对 ...

  6. PHP之语句

    前面的话 任何 PHP 脚本都是由一系列语句构成的.一条语句可以是一个赋值语句,一个函数调用,一个循环,一个条件语句或者甚至是一个什么也不做的语句(空语句).语句通常以分号结束.此外,还可以用花括号将 ...

  7. c++ 重载、重写、重定义(隐藏)

    1.重载overload:函数名相同,参数列表不同. 重载只是在类的内部存在,或者同为全局范围.(同名,同参函数返回值不同时,会编译出错.因为系统无法知晓你到底要调用哪一个.)   2.重写overr ...

  8. MQTT-SN协议乱翻之功能描述

    前言 紧接上文,这是第三篇,主要是对MQTT-SN 1.2协议进行总体性功能描述. 嗯,这一部分可以结合着MQTT协议对比着来看. 网关的广播和发现 网关只能在成功连接到MQTT Server之后,才 ...

  9. devstack with neutron 参考文献

    http://networkstatic.net/installing-openstack-ml2-neutron-plugin-devstack-fedora/ https://wiki.opens ...

  10. LeetCode——House Robber

    Description: You are a professional robber planning to rob houses along a street. Each house has a c ...