思路:

使用两个节点。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. java学习笔记5--类的方法

    接着前面的学习: java学习笔记4--类与对象的基本概念(2) java学习笔记3--类与对象的基本概念(1) java学习笔记2--数据类型.数组 java学习笔记1--开发环境平台总结 本文地址 ...

  2. 移动端的emoji表情符号插入MySQL数据库失败

    插入数据时候报了错:### Error updating database. Cause: Java.sql.SQLException: Incorrect string value: ‘\xF0\x ...

  3. mysql5.0.x统计每秒增删改查替换数及系统每秒磁盘IO

    转载自:http://blog.chinaunix.net/uid-9370128-id-393082.html 1. mysql 5.0.x 统计每秒增,删,改,查,替换数  mysql 的show ...

  4. RS报表设计采用Total汇总过滤出错

    错误信息: DMR 子查询计划失败,并产生意外错误.: java.lang.NullPointerException 如图 原因是在RS过滤器中添加了: total([门诊人次] for [明细科室] ...

  5. Andrew Ng机器学习笔记+Weka相关算法实现(四)SVM和原始对偶问题

    这篇博客主要解说了Ng的课第六.七个视频,涉及到的内容包含,函数间隔和几何间隔.最优间隔分类器 ( Optimal Margin Classifier).原始/对偶问题 ( Primal/Dual P ...

  6. SCU 4313 把一棵树切成每段K个点 (n%k)剩下的点不管

    题目链接:http://cstest.scu.edu.cn/soj/problem.action?id=4313 判断是不是存在拆图得到新连通分支的点个数是K的倍数 注意一个点所连的边只能被切一条 # ...

  7. function.length和arguments的区别

    function.length:接收到函数体外的参数计算长度 arguments:接收到函数体内的参数计算长度 /** * 函数参数长度和伪数组(arguments)长度不一样! -> 接收到函 ...

  8. VMware Workstation的网络连接方式:NAT、桥接和Host Only

    安装完VMware Workstation后会自动生成两个网络连接:VMware Network Adapter VMnet8 和 VMware Network Adapter VMnet1(通常称为 ...

  9. MySQL 工具

    MySQL 客户端工具: 1:mysql       #mysql的功能和Oracle的sqlplus一样,它为用户提供一个命令行接口来管理Mysql服务器. 2:mysqladmin #mysqla ...

  10. Java泛型中extends和super的理解

    作者:zhang siege链接:https://www.zhihu.com/question/20400700/answer/91106397来源:知乎著作权归作者所有.商业转载请联系作者获得授权, ...