思路:

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

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

实现代码:

  1. public bool HasCycle(ListNode head) {
  2. // - for null node , false
  3. if(head == null || head.next == null){
  4. return false;
  5. }
  6. if(head.val != head.next.val && head.next.next == null){
  7. return false;
  8. }
  9.  
  10. var slow = head;
  11. var fast = head;
  12.  
  13. while(true) {
  14. slow = slow.next;
  15. if(fast.next != null){
  16. fast = fast.next.next;
  17. }
  18. else{
  19. return false;
  20. }
  21.  
  22. if(slow == null || slow.next == null || fast == null || fast.next == null) {
  23. return false;
  24. }
  25.  
  26. if(slow.val == fast.val && slow.next.val == fast.next.val){
  27. return true;
  28. }
  29. }
  30. return false;
  31. }

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. Android -- 再来一发Intent

    之前写过一篇Intent的博客,主要说了一下隐式意图. 传送门:<Android -- Intent> Intent对象构成 Component name.Action.Data.Cate ...

  2. 在Docker和Kubernetes上运行MongoDB微服务

    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟.容器是完全使用沙箱机制,相互之间不会有任何接 ...

  3. 超棒的HTML5/CSS3单页面响应式模板(支持Bootstrap)

    在线演示 使用Bootstrap实现的响应式单页面模板. 桌面效果: 移动设备效果: 阅读全文:超棒的HTML5/CSS3单页面响应式模板(支持Bootstrap)

  4. 异步任务,HttpContext.Current为null解决办法

    最近在开发一个后台管理系统项目,为了提高登录的速度,就把记录登录日志放到一个异步任务里面. Action taskAction = () => { SaveLog(); }; Task task ...

  5. UNIX网络编程读书笔记:端口号、套接口对和套接口

    端口号 端口号(port number):16位整数,用来区分不同的进程. 服务器使用的端口号:TCP和UDP定义了一组众所周知的端口(well-known port),用于标识众所周知的服务. 客户 ...

  6. 【转】TCP(协议号6)的方方面面

    转:http://blog.sina.com.cn/s/blog_6002b97001018fxh.html 第一:TCP连接的建立(也就是所谓的三次握手)过程. 第一次握手:建立连接时,客户端发送s ...

  7. tomcat服务器开启gzip功能的方法

    http://blog.csdn.net/wang_159369/article/details/8107163 tomcat5.0以后的版本是支持对输出内容进行gzip格式的压缩的.该功能默认是关闭 ...

  8. IOS客户端Coding项目记录(二)

    9:第三方插件整理 JSON转实体:jsonModel https://github.com/icanzilb/JSONModel/ 美化按键:BButton https://github.com/m ...

  9. C++(一)——HelloWorld

    之前学C.学Python,学的比較多的是Java,作为大家口中更强大的C++,要学学,这次的话,以了解主要的特性和做个小游戏作为目标吧. 1)HelloWorld Eclipse执行C++之Launc ...

  10. 【转发】jQuery1.9.1至最高版本针对checkbox的调整

    在jquery 1.8.x中的版本,我们对于checkbox的选中与不选中操作如下: 判断是否选中 $('#checkbox').prop('checked') 设置选中与不选中状态: $('#che ...