题目:

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

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

题解:

这道题连带着II是很经典的,在看CC150时候,纠结这个问题纠结了很久。在读了很多网上资料还有书的讲解以及和别人讨论之后,对这个专题终于明白了。

这一问只需要判断链表是否有环。

当链表没有环时是很好判断的,让一个指针一直往后走,遇见null了自然就没有环。

而如何判断有环,那么就需要引入Faster和Slower的概念了(也是一种双指针方法)。顾名思义,同个时间Faster走的比Slower走的多。一般来说,Slower每次走一步,Faster每次走2步(通常这个概念可以判断链表中间点)。在这里,Faster和Slower同时从起点遍历链表,如果有环那么Slower和Faster肯定会相遇。

为什么他俩肯定能相遇呢?万一一个把一个超了但是没相遇咋办?

直觉和生活经验告诉我,他俩肯定能相遇,比如在操场跑圈,一个快的一个慢的同时开始跑,一直跑,快的肯定能跟慢的相遇。不过有更严谨的说法就更有说服力了。

下面我就引用一下CC150里面外加我的完善来说明怎么证明的这个问题:

假设Faster确实把Slower超了而且他俩还没相遇(类似Faster一下迈了2步,Slower一下迈了一步,Faster超了Slower,但是俩人并没遇上)。那么就假设Faster现在在 i+1 位置而Slower在 i 位置。那么前一时刻,Slower肯定是在 i-1 位置,而Faster肯定在(i+1)-2位置,所以前一时刻,俩人都在 i-1 位置,相遇了。

还有一种情况就是Faster在i+2位置而slower在i位置,那么前一时刻,Faster在i位置,而Slower在 i-1位置。这样问题又回归到上面那种情况了(再往前一时刻,Faster在i-2位置,Slower在i-1-1位置,相遇)。

所以,这就证明Runner和Faster在有环的链表中肯定会相遇。

代码工作就很简单了,如下:

 1     public boolean hasCycle(ListNode head) {
 2         if(head == null || head.next == null)
 3             return false;
 4         
 5         ListNode Faster = head, Slower = head;
 6         
 7         while(Faster.next!=null && Faster.next.next!=null){
 8             Slower = Slower.next;
 9             Faster = Faster.next.next;
             
             if(Faster == Slower)
                 return true;
         }
         return false;
     }

Linked List Cycle leetcode java (链表检测环)的更多相关文章

  1. 142 Linked List Cycle II(如果链表有环,找到入口结点Medium)

    题目意思:如果有环,返回入口结点 思路:先判断有没环,再计算环的结点数,然后p1指向头,p2往后移结点次数,p1.p2相遇为入口结点 ps:还是利用指针间距这个思路 /** * Definition ...

  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 & Linked List Cycle II——单链表中的环

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

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

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

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

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

  6. leetcode 142. Linked List Cycle II 环形链表 II

    一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点  head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...

  7. 【LeetCode】Linked List Cycle II(环形链表 II)

    这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? ...

  8. 142 Linked List Cycle II 环形链表 II

    给一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null.说明:不应修改给定的链表.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/proble ...

  9. 141. Linked List Cycle - LeetCode

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

随机推荐

  1. Java设计模式 - 持续更新

    注意,此博客来源于我的 OneNote 笔记本 因此属于图片形式进行展示,这意味着你可以: 不经过我的同意进行保存 不经过我的同意进行发布 我仍然希望搬运时留一个网址指明来处:我的博客园 多谢!以下是 ...

  2. CSUOJ 1087 就多了两分钟

    Description Yucept21和他的室友Zyn因为宿舍没电去网吧上网,上了27分钟,Cs打电话来说来电了.所以Yucept21在第29分钟下机了,上网的费用是一块钱,然后Zyn墨迹了两分钟, ...

  3. 1021 Deepest Root (25)(25 point(s))

    problem A graph which is connected and acyclic can be considered a tree. The height of the tree depe ...

  4. [ 原创 ]学习笔记-三种向ListView中填充简单文本的方法

    Android 中ListView是很重要的一块内容 掌握ListView的基本用法 对学习安卓起着举足轻重的作用 今天就介绍一下三种向ListView 填充简单文本的方法 填充其他数据类型的用法之后 ...

  5. 为了增强团队的协作和高效开发,提升代码质量,TGideas团队一起制订的代码规范。主要包括五部分内容:PC规范、移动端规范、性能优化、CP规范、其他项目规范

    http://tguide.qq.com/main/index.htm

  6. android 视频 2017

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha

  7. mac下递归创建ctags报错: "illegal option -- R"

    在mac系统下不论是使用vim还是sublime text2的ctags插件都会碰到“illegal option -- R”这个错误,原因是mac使用的是自己的ctags,而我们通常在linux或w ...

  8. python及其模块下载集合

    1)python平台 https://www.python.org/downloads/ 2)打包工具 cx-freeze(python3以上版本打包工具) http://cx-freeze.sour ...

  9. Null 和 Undefined

    在JavaScript中存在这样两种原始类型:Null与Undefined.这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined? Undef ...

  10. c++ 常见网络协议头

    //NTP协议 typedef struct _NTP_HEADER { uint8_t _flags;//Flags 0xdb uint8_t _pcs;//Peer Clock Stratum u ...