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. Oracle常用sql

    Oracle不像Sqlserver,并没有提供l默认约束,但提供了默认值,效果一样.--------------------------- 在建表时设置默认约束-------------------- ...

  2. 33.Android之Fragment学习

    Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...

  3. poj1056 (Trie入门)寻找字符串前缀

    题意:给你一堆字符串,问是否满足对于任意两个字符串a.b,a不是b的前缀 字典树==前缀树==Trie树 trie入门题,只用到了insert和query操作 #include <cstdio& ...

  4. 求DAG上两点的最短距离

    Problem 给出一个不带边权(即边权为1)的有向无环图(unweighted DAG)以及DAG上两点s, t,求s到t的最短距离,如果无法从s走到t,则输出-1. Solution DFS,BF ...

  5. poj1733Parity game

    Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7288   Accepted: 2833 Descr ...

  6. hdu 1201 18岁生日

    #include <stdio.h> int r(int y) { return (y%4==0&&y%100!=0)||(y%400==0); } int f(int y ...

  7. jquery uploadify 使用

    /*进度条框*/ .shangchuantishikuang { border: 7px solid #74A5BF; background-color: white; font-size: 14px ...

  8. Android-深入理解android自定义属性(AttributeSet,TypedArray)

    属性 自定义属性,首先要定义出来属性,我们新建一个attrs.xml: <?xml version="1.0" encoding="utf-8"?> ...

  9. linux 的iptables防火墙

    .a文件就是*.o文件的集合, 是object文件的归档文件, 所以, 用nm -A  ???.a看到的 symbolic符合名称都是 相应的,  包含的  .o文件.... linux 2.4内核中 ...

  10. 错误 1 未知的服务器标记“asp:ScriptManager”。

    如题 ... 解决方案 :将 <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=ne ...