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

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

解题思路:

由于不让用extra space,所以用一个快指针和一个慢指针,快指针一次移动两步,慢指针一次移动一步,只要快指针赶上慢指针证明纯在loop,JAVA实现如下:

    public boolean hasCycle(ListNode head) {
ListNode fast=head,slow=head;
while(fast!=null){
slow=slow.next;
ListNode temp=fast.next;
if(temp==null)
return false;
fast=temp.next;
if(fast==slow)
return true;
}
return false;
}

Java for 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 链表中的环

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

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

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

  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. Java for LeetCode 142 Linked List Cycle II

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

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

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

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

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

随机推荐

  1. 用php生成数据字典

    <?php header("Content-type: text/html; charset=utf-8"); $dbserver = "localhost&quo ...

  2. BZOJ-4300 绝世好(蛋疼)题 DP(递推)

    翻zky学长的blog时翻出来的..... 4300: 绝世好题 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 736 Solved: 393 [Sub ...

  3. 【poj1050】 To the Max

    http://poj.org/problem?id=1050 (题目链接) 题意 求二维最大子矩阵 Solution 数据好像很水,N最大才100,N^4大暴力都可以随便水过. 其实有N^3的做法.枚 ...

  4. UVA 1398 Meteor

    传送门 Solution: 记一颗流星在视野内的时间段为(L, R), 为了使所有(L, R)都取整数,首先将坐标放大. 放大倍数可取为 LCM(1, 2, ..., 10)= 2520 接着计算:从 ...

  5. py替换掉换行符

    for line in file.readlines(): line=line.strip('\n')

  6. IOS基础之 (三) 类的声明和对象的创建

    一 OC类的声明和实现语法 1.接口的声明 @interface NewClassName: ParentClassName { 实例变量 ... } 方法的声明 ... @end //...表示省略 ...

  7. WPF中嵌入Office编辑器(支持Word、Excel、PPT、Visio等)

    现在有一个项目,需要使用wpf做一个简单的客户端,用来生成word.excel.ppt.visio等文档,这就需要能够在wpf中嵌入office的编辑器,并对office文档进行编辑. 在网上搜索了一 ...

  8. centos6.3配置MFS服务器

    一.简介 MooseFS(Moose File System,mfs)是一种分布式文件系统,它将数据分布在网络中的不同服务器上,支持FUSE(用户空间文件系统Filesystem in Userspa ...

  9. java中抽象类与接口中方法访问修饰符问题 (

    1.抽象类中的抽象方法(其前有abstract修饰)不能用private.static.synchronized.native访问修饰符修饰.原 因如下:抽象方法没有方法体,是用来被继承的,所以不能用 ...

  10. 改变了Tomcat路径后无法卸载和重装的解决办法

    错误如下: ---------------------------Apache Tomcat Setup---------------------------Failed to install Tom ...