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

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

题意:

判断一个链表是否有环。

解决方案:

双指针,快指针每次走两步,慢指针每次走一步,

如果有环,快指针和慢指针会在环内相遇,fast == slow,这时候返回true。

如果没有环,返回false.

/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head, slow = head;
if(head == null || head.next == null) return false; while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next; if(fast == slow){
return true;
}
} return false;
}
}

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 、 142. Linked List Cycle II

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

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

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

  7. Java for 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 ex ...

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

  9. Python [Leetcode 141]Linked List Cycle

    题目描述: Given a linked list, determine if it has a cycle in it. 解题思路: 快的指针和慢的指针 代码如下: # Definition for ...

随机推荐

  1. 简单实现Windows服务 TopShelf

    Nugut安装 log4net 和 topShelf 1)ServiceRunner类 using log4net;using Topshelf; class ServiceRunner : Serv ...

  2. WinForm------TreeList实现鼠标经过节点背景色改变

    转载: http://www.cnblogs.com/zfanlong1314/archive/2012/06/26/2564124.html

  3. 如何以nobody用户执行命令?

    最近在logstash中使用nobody用户启动logstash,一想,nobody用户的shell不是/sbin/nologin吗? 不能登录执行命令呀? 于是看了一下它的启动脚本,是使用其他方式进 ...

  4. LDA(主题模型算法)

    LDA整体流程 先定义一些字母的含义: 文档集合D,topic集合T D中每个文档d看作一个单词序列< w1,w2,...,wn >,wi表示第i个单词,设d有n个单词.(LDA里面称之为 ...

  5. CentOS 6.x安装Metasploit

    现在开始安装Metasploit框架,前面的包安装成功之后,我们需要再安装一些Metasploit依赖的Ruby库,命令如下: gem install wirble pg sqlite3 msgpac ...

  6. 自然语言17_Chinking with NLTK

    https://www.pythonprogramming.net/chinking-nltk-tutorial/?completed=/chunking-nltk-tutorial/ 代码 # -* ...

  7. linux手动或者自动启动oracle11g的服务 Oracle 自动启动脚本

    手动启动: [oracle@localhost ~]$ sqlplus SQL*Plus: Release 11.2.0.1.0 Production on Wed Mar 26 23:39:52 2 ...

  8. JavaWeb学习笔记——Tomcat相关

    Tomcat目录分析 1.bin 存放启动和关闭Tomcat的脚本文件 2.conf  存放Tomcat服务器的各种配置文件 3.lib  存放Tomcat服务器的支持jar包 4.logs  存放T ...

  9. Hmmer安装与使用

    Hmmer的安装与使用   从功能基因研究的角度来讲,相关的搜索,比如从序列数据库中,找同源的序列,或者对一个对一个新的基因功能进行鉴定,使用hmmer比使用blast有着更高的灵敏度已经更高的搜索速 ...

  10. 关于Unity动态物体无法向使用使用custom shader和lightmap的物体投射阴影

    最近在做unity shader forge和marmoset的优化,TA那边遇到了一个阴影显示的问题,具体如下:   在Forward Rendering状态下,静态场景使用了是shader for ...