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

Follow up:

Can you solve it without using extra space?

原题链接:https://oj.leetcode.com/problems/linked-list-cycle/

题目:给定一个链表。推断它是否有环。

继续:

你能不用额外的空间解决吗?

思路:使用两个指针fast,slow,fast每次向前走两步,slow每次向前走一步。假设二者相应的节点相等。即存在环。

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

LeetCode——Linked List Cycle的更多相关文章

  1. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  2. [LeetCode] Linked List Cycle II 单链表中的环之二

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

  3. [LeetCode] Linked List Cycle 单链表中的环

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

  4. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  5. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  6. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  7. LeetCode: Linked List Cycle 解题报告

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  8. LeetCode Linked List Cycle 解答程序

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...

  9. [Leetcode] Linked list cycle ii 判断链表是否有环

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

  10. [LeetCode] Linked List Cycle II, Solution

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

随机推荐

  1. Win7下通过easyBCD引导安装Ubuntu14.04

    Ubuntu14.04作为目前最新版本的ubuntu系统,相信很多人都想在自己的电脑上安装一下,然而系统的安装方法各式各样,u盘法.grub引导法等等,这里我将介绍在win7系统下用easyBCD软件 ...

  2. sql数据库优化技巧汇总

    (转)SQL 优化原则 一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用系统提交实际应用后,随着 ...

  3. POJ 2513 Colored Sticks 字典树、并查集、欧拉通路

    Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...

  4. VS 调试相关

    最近用VS2013 调试遇到的一点小问题,老年痴呆做一下记录. 1. IIS 用 w3wp.exe 调试: IIS 中的文件系统与工程的保持一致,否则断点不会命中: 发布文件系统后,重启站点对应的应用 ...

  5. Android Audio 分析

    一.架构 二.MediaServer初始化 所有的media服务都在进程mediaserver里.其代码在framework/base/media/mediaserver/main_mediaserv ...

  6. 趣味C程序100.9 绘制杨辉三角

    说明:1.本问题来源于<C语言经典.趣味.实用程序设计编程百例精解>,所有程序为本人自己编写.与原程序不同之处作有标记. 2.本系列所有程序均使用codeblocks编译,操作系统为Win ...

  7. spring与mybatis,strut2整合连接sqlserver不的不说的那点事儿

    今天在通过spring与mybatis整合中,想连接下公司用的sqlserver数据库,结果使用Junit测发现没连上,于是就有了下面的问题: 准备工作都已经做好了 web中spring的监听配置了 ...

  8. AndroidStudio 更新gradle Error:Failed to complete Gradle execution. Cause: Connection reset

      Android Studio 报错:Error:Failed to complete Gradle execution.  Cause: Connection reset.把最新可以运行的项目中g ...

  9. [BZOJ 1047] [HAOI2007] 理想的正方形 【单调队列】

    题目链接:BZOJ - 1047 题目分析 使用单调队列在 O(n^2) 的时间内求出每个 n * n 正方形的最大值,最小值.然后就可以直接统计答案了. 横向有 a 个单调队列(代码中是 Q[1] ...

  10. Web-Scale-IT 到底是啥?

    Gartner 对 2015 年 10 大 IT 趋势的预测中有一个词条为:Web Scale IT.我们跟随 Matthias Ankli 来了解一下究竟什么是 Web Scale IT.本文译自 ...