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. ionic2中跨页面回传值

    1.在跳转到新页面时传入一个contactsCallback的参数,在该参数的函数定义中做出一个承诺. 注意:最开始我本来是采用如下图方式的,但是很不幸,出现了问题,问题所在就是关于这个this的作用 ...

  2. gradle 代理设置

    可以对gradle全局设置变量,也可以针对单个项目进行配置. 只需要在.gradle目录下创建一个gradle.properties文件,文件中按照如下格式添加代理配置 systemProp.http ...

  3. git 解决冲突问题

    问题描述: 一般来说,只要多人提交同一仓库,必然会有冲突的发生. Git解决 利用git解决冲突很简单,最常规的方法莫过于工作之前先更新整个project,完成之后及时提交.然而尽管这样做,也难免有人 ...

  4. 《DSP using MATLAB》Problem 2.9

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  5. 【转】inittab文件

    原文网址:http://blog.csdn.net/shuaishuai80/article/details/6202482 一.inittab文件背景(1)init进程的作用   使用uboot下载 ...

  6. oracle12c之 表空间维护总结

    1.1.创建永久表空间 In the CDB:SQL> CONNECT system@cdb1SQL> CREATE TABLESPACE cdb_users DATAFILE'/home ...

  7. stacks and queues--codility

    lesson 7: stacks and queues 1. Nesting 2. StoneWall 3. Brackets 4. Finsh lesson 7: stacks and queues ...

  8. 在Eclipse中搭建Python开发环境

    在Eclipse中搭建Python开发环境 来自: http://hi.baidu.com/hqwfreefly/blog/item/2543181d0afd9604314e150e.html 前言 ...

  9. Data_Structure03-栈和队列

    一.学习总结 1.写出你认为本周学习中比较重要的知识点关键词 ·抽象数据类型 ·栈和队列 2.思维导图 二.PTA实验作业 选题: 1.7-1 jmu-字符串是否对称(20 分) 2.7-4(选做) ...

  10. java 查询solr时间格式

    solr时间格式是2015-07-06T00:00:00.0Z,所以下面是把当前时间做转换 SimpleDateFormat format = new SimpleDateFormat("y ...