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

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

题意:

给定一个链表,判断是否循环

思路:

快慢指针

若有环,则快慢指针一定会在某个节点相遇(此处省略证明)

代码:

 public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
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. 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. 141. Linked List Cycle(判断链表是否有环)

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

  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. 141 Linked List Cycle(判断链表是否有环Medium)

    题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...

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

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

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

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

随机推荐

  1. Oracle(一)安装

    一.到官网或者哪里去下载Oracle,我下的是winX64的11g版本 官网:https://www.oracle.com/technetwork/database/enterprise-editio ...

  2. 开发中PG,PL,SE,PM都是什么意思

    注:参考连接:http://blog.csdn.net/ahutqi/article/details/42104361 项目最顶层是项目负责人,接下来项目会落实到pm(项目经理PM),项目经理将任务分 ...

  3. HTML第一课——基础知识普及【1】

    请关注公众号:自动化测试实战 HTML概念及编写规范 html叫做超本文标记语言,注意它知识标记语言,不是编程语言. 编写规范: 由标记(html, div, p, h1等)组成 标记成对出现(< ...

  4. ringojs java jar 集成使用

    ringojs 可以方便进行java 代码的集成,我们可以把下载的jar包放到classpath,后者ringojs 的lib 目录 也可以进行代码编写 测试代码 集成了java 的一个hashid ...

  5. eclipse adt logcat过滤用法

    点击Save Filters的加号,by Log Tag如下就可以只看2dx程序出来的debug信息了

  6. (原创)AP6212移植到AM335X自主开发板上

    转载请指明出处. 参考<关于AM335X移植SDIO WIFI的简易教程> http://www.deyisupport.com/question_answer/dsp_arm/sitar ...

  7. SQL注入与安全防护---以PHP为例

    一.什么是sql注入: 简单来说,当我们从前端的用户表单数据往后台数据库传输时,可能用户表单数据中的某些数据,会跟我们的后台发生“有机”反应,从而导致发生一些数据库的异常操作. 举个例子吧,以简单的用 ...

  8. [C++ Primer] 第2章: 变量

    初始化与赋值 当对象在创建时获得了一个特定的值, 我们就说这个对象被初始化了. 在使用=号时, 如果创建了新的对象, 则为初始化而非赋值. 初始化不是赋值, 初始化的含义是创建变量时赋予其一个初始值, ...

  9. A Newbie’s Install of Keras & Tensorflow on Windows 10 with R

    This weekend, I decided it was time: I was going to update my Python environment and get Keras and T ...

  10. 峰Spring4学习(8)spring对事务的支持

    一.事务简介: 二.编程式事务管理: 例子 1.需求:模拟转账,张三向李四转账50元: 数据库中存在t_count表: 代码实现: BankDao.java: package com.cy.dao; ...