思路:

使用两个节点。slow和fast,分别行进1步和2步。假设有相交的情况,slow和fast必定相遇;假设没有相交的情况,那么slow或fast必定有一个为null

相遇时有两种可能:
1. 仅仅是节点相交的情况,即:slow == fast可是 slow.next != fast.next
2. 链表中存在环,即slow == fast 并且 slow.next == next

实现代码:

public bool HasCycle(ListNode head) {
// - for null node , false
if(head == null || head.next == null){
return false;
}
if(head.val != head.next.val && head.next.next == null){
return false;
} var slow = head;
var fast = head; while(true) {
slow = slow.next;
if(fast.next != null){
fast = fast.next.next;
}
else{
return false;
} if(slow == null || slow.next == null || fast == null || fast.next == null) {
return false;
} if(slow.val == fast.val && slow.next.val == fast.next.val){
return true;
}
}
return false;
}

LeetCode -- 推断链表中是否有环的更多相关文章

  1. Q:判断链表中是否存在环的相关问题

    问题:如何判断一个单向链表中是否存在环? 例如: 链表中存在环(B-->D): <-- <--^ | | v | A-->B-->C-->D 链表中不存在环: A- ...

  2. 查找链表中是否有环linked-list-cycle

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

  3. LeetCode -- 删除链表中值为k的元素

    本题目比較直接,一次遍历遇到匹配的元素直接删除(通过n.next = n.next.next)就能够了,仅仅是须要考虑到:1.首节点的情况2.末节点的情况 下面为实现: public ListNode ...

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

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

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

  6. LeetCode之链表总结

    链表提供了高效的节点重排能力,以及顺序性的节点访问方式,并且可以通过增删节点来灵活地调整链表的长度.作为一种常用的数据结构,链表内置在很多高级编程语言里面.既比数组复杂又比树简单,所以链表经常被面试官 ...

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

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

  9. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

随机推荐

  1. grafana-zabbix图形简单配置

    连接zabbix数据库 加入dashboard Home--Add--加入dashboad 设置dashboad 设置名字,和标签tag,tag可在输入后回车加入多个 加入简单的一张图,測试能否获取到 ...

  2. FrameWork模型中引入宏函数报错解决方法

    如下图在Framework的一个简单维度中加入宏函数 解决办法如下图 step1: step2: PS :Cognos 10.1.1中 在cognos connection中创建数据源,为什么没有od ...

  3. source insight中{}自动缩进的调整

    默认的自动缩进非常难看,解决方法如下: 菜单栏 -> Options -> document options ->点击右侧的 “Auto Indent...”按钮 将右侧" ...

  4. 初次使用IntelliJ IDEA

    一.认识IDEA IDEA 全称IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持 ...

  5. Servlet实现文件上传,可多文件上传

    一.Servlet实现文件上传,需要添加第三方提供的jar包 接着把这两个jar包放到 lib文件夹下: 二: 文件上传的表单提交方式必须是POST方式, 编码类型:enctype="mul ...

  6. JUnit 3.8 演示递归删除文件目录的 测试类程序 .

    用递归方式来实现删除硬盘的文件或目录(空文件夹) 首先要找到递归的入口及出口,这点很重要,成败在此,呵呵! 代码实现: import java.io.File ; class RecursionDel ...

  7. iOS网络监控— BMReachability

    1. What's BMReachability? BMReachability是基于AFNetworking的Reachability类封装的监听网络状态变化的组件. 它在AF提供的无网络/wifi ...

  8. ReadOnly field saved with NULL value

    On CRM opportunity form view, i added readonly="1" for probability field. When i saved, wh ...

  9. spring mvc 返回json的配置

    转载自:http://my.oschina.net/haopeng/blog/324934 springMVC-servlet.xml 配置 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  10. C++方式解析时间字符串和计算时间

    #include "StdAfx.h"#include "MySetTimeByVT.h" #include <ATLComTime.h>#incl ...