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

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

Solution: solve the problem with one and two steps

no cycle case: the faster pointer(two step) reaches the null first

cycle : slower == faster

Caution: check the null case

/**
* 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) {
if(head == null) return false;
if(head.next == null) return false;
//with extra space
//a -> b ->c -> d -> a;
ListNode one = head;
ListNode two = head;
while(two.next != null && two.next.next !=null){
one = one.next;
two = two.next.next;
if(one==two) return true;
}
return false;
}
}

141. Linked List Cycle (amazon)的更多相关文章

  1. 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)

    题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...

  2. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

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

  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. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  5. 141. Linked List Cycle【easy】

    141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

  6. 141. Linked List Cycle - LeetCode

    Question 141. Linked List Cycle Solution 题目大意:给一个链表,判断是否存在循环,最好不要使用额外空间 思路:定义一个假节点fakeNext,遍历这个链表,判断 ...

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

  8. 【LeetCode】141. Linked List Cycle (2 solutions)

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

  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. 解决IDEA卡顿问题及相关基本配置

    https://blog.csdn.net/u013068377/article/details/54316965 https://blog.csdn.net/u014527619/article/d ...

  2. python3+selenium获取列表某一列的值

    python3+selenium获取列表某一列的值 我们在坐自动化测试时,我们可能不想单纯的想验证一个选项卡,我们让脚本随机选择一个选项进行接下来的操作.例如我们想获取列表某一列的某一个数据(随机的) ...

  3. 19. Remove Nth Node From End of List(C++,Python)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  4. 工作ui(2)

    做完整个小Demo整理的一些方法和踩过的miniUI的坑,分享出来希望大家批评指正,共同进步. 1.动态创建列:尽量不要直接在html文件里创建列,动态设置在js文件里方面添加.修改等. 首先把列定义 ...

  5. JavaSE---使用反射生成JDK动态代理

    1.概述 1.1 在Java.lang.reflect包下,提供了Proxy类.InvocationHandler接口,使用它们可以生成JDK动态代理类或动态代理对象: 1.2 [Proxy类] 1. ...

  6. Silverlight FullScreen 全屏

    <UserControl x:Class="FullScreen.MainPage" xmlns="http://schemas.microsoft.com/win ...

  7. Deep Learning 和 Knowledge Graph howto

    领军大家: Geoffrey E. Hinton http://www.cs.toronto.edu/~hinton/ 阅读列表: reading lists and survey papers fo ...

  8. RTT学习之BSP

    ---恢复内容开始--- 一 根据相近型号的demo BSP进行修改制作自己的BSP https://github.com/RT-Thread/rt-thread/blob/master/bsp/st ...

  9. react做股票、期货交易遇到的问题(不完全是react)及解决方法。

    公司项目主要是做股票及期货行情展示及交易,h5相应的做了一些功能---可以看行情图及模拟交易,实盘交易存在一定的风险,老板希望做自己的产品,这样h5就尴尬了,不过没关系,项目里还是有一定技术含量的-- ...

  10. tck/tl 以及expect脚本

    最近有用到,利用expcet脚本自动登录到远程服务器并提权执行脚本. 搜集的知识如下: tcl/tk参考——列表操作lindex expect脚本解释 代码如下 #!/usr/bin/expect - ...