思路:

使用两个节点。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. .net维护的一些心得

    做了三个月的.net的开发,其实一直在做一个维护加二次开发的工作.现在这个项目告一段落,就此总结下我的所学所感吧.总的来说,.net和JAVA还是有许多地方是不同的,比如.net中的数据绑定问题,已经 ...

  2. 解决Vue用Nginx做web服务器报错favicon.ico 404 (Not Found)的问题

    有多种解决方案 1.vue静态资源 vue中为网页增加favicon的最便捷的方式为使用link标签 <link rel="shortcut icon" type=" ...

  3. 【php】Apache无法自己主动跳转却显示文件夹与php无法连接mysql数据库的解决方式

    一.Apache无法自己主动跳转却显示文件夹 Apache无法自己主动跳转却显示文件夹这是由于Apacheserver下conf/httpd.conf没有配置好,其默认是不跳转,直接显示文件夹 首先, ...

  4. HTML5 本地存储形式

    1.sessionStorage 2.localStorage 3.Database Storage 4.globalStorage 5.兼容性 参考文献 本地持久化存储一直是本地客户端程序优于 we ...

  5. cocos2d-x eclipse + NDK 自动编译配置

    文章来自:http://www.cocos2dres.com/view.asp?id=90 1. 准备工作: android NDK    r8版本 ADT Bundle 下载地址:http://de ...

  6. VMware 9.0.1安装Mac OS X Mountain Lion 10.8.2

    原地址:http://zengwu3915.blog.163.com/blog/static/278348972013117114742496/ 所需软件1.VMware Workstation Bu ...

  7. swift -类的定义及使用

    // // main.swift // ClassDemo-06 // import Foundation println("Hello, World!") //类的定义 Pers ...

  8. gre tunnel搭建

    应用场景: 客户端(client)与服务器A在同一个运营商网络,应用部署在服务器B,服务器A .B之间建立tunnel,A设置dnat,client通过访问A的8000端口来访问服务器B,B返回的响应 ...

  9. vnc/route/ifconfig 配置

    重启网卡的方法: 1 network 利用root帐户 # service network restart ############################################## ...

  10. js 替换json对象中的键名

      js 替换json对象中的键名 CreateTime--2018年3月30日15:38:50 Author:Marydon 情景描述: 有个json数组,现在需要将json对象中的key替换掉,值 ...