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. oc中的各种遍历(迭代)方法

    转载自文顶顶老师的博客:http://www.cnblogs.com/wendingding/p/5251937.html 说明: 1)该文简短介绍在ios开发中遍历字典.数组和集合的集中常见方式 2 ...

  2. [转载]2014年10月26完美世界校招两道java题

    public class VolitileTest { volatile static int count=0; public static void main(String args[]){ for ...

  3. MySQL性能优化(十)-- 主从复制(一主多从)

    环境准备: Linux1(matser) Linux2(slave) Linux3(slave) 搭建 1.先清空原来的master和slave配置 reset master; 2.

  4. Python 列表表达式与生成器表达式

    列表表达式: (1) 语法1:[表达式 for 变量 in 列表],表示把得到的每一个变量值都放到 for 前面的表达式中计算 ,然后生成一个列表(2) 语法2:[表达式 for 变量 in 列表 i ...

  5. Android AndroidManifest.xml配置文件

    AndroidManifest.xml配置文件介绍本质:AndroidManifest.xml是整个应用的主配置清单文件.包含:该应用的包名.版本号.组件.权限等信息.作用:记录该应用的相关配置信息. ...

  6. Linux获取当前目录名,shell获取当前目录名

    想把当前目录名保存到一个变量中,然后用在别的地方 ${PWD##*/} 测试: cd /var/log/squid echo ${PWD##*/} 还有很多种方法,请参考这个老外写的: http:// ...

  7. Python-Numpy的tile函数用法

    1.函数的定义与说明 函数格式tile(A,reps) A和reps都是array_like A的类型众多,几乎所有类型都可以:array, list, tuple, dict, matrix以及基本 ...

  8. Android 设置wifi共享电脑服务器资源

    其实win7本身就自带无线热点的功能,按下面的方法设置. 开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让电脑变成无线路由器,实现共享上网,节省网费和路由器购买 ...

  9. LeetCode——Populating Next Right Pointers in Each Node

    Description: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; Tree ...

  10. 条件注释判断IE浏览器版本

    lt,lte,gt,gte分别表示什么 lt:小于当前版本 lte:小于或等于当前版本,包括本身 gt:大于当前版本 gte:大于或等于当前版本,包括本身 使用格式 // 如IE9以下(不包括IE9加 ...