• Question :

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

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

  • Anaylsis :
    •   

      首先,比较直观的是,先使用Linked List Cycle I的办法,判断是否有cycle。如果有,则从头遍历节点,对于每一个节点,查询是否在环里面,是个O(n^2)的法子。但是仔细想一想,发现这是个数学题。

      如下图,假设linked list有环,环长Y,环以外的长度是X。

      现在有两个指针,第一个指针,每走一次走一步,第二个指针每走一次走两步,如果他们走了t次之后相遇在K点

      那么       指针一  走的路是      t = X + nY + K        ①

      指针二  走的路是     2t = X + mY+ K       ②          m,n为未知数

      把等式一代入到等式二中, 有

      2X + 2nY + 2K = X + mY + K

      =>   X+K  =  (m-2n)Y    ③

      这就清晰了,X和K的关系是基于Y互补的。等于说,两个指针相遇以后,再往下走X步就回到Cycle的起点了。这就可以有O(n)的实现了。

    • from : http://fisherlei.blogspot.tw/2013/11/leetcode-linked-list-cycle-ii-solution.html
  • Code :
    •   

      /**
      * Definition for singly-linked list.
      * struct ListNode {
      * int val;
      * struct ListNode *next;
      * };
      */
      struct ListNode *detectCycle(struct ListNode *head) {
      if(!head) return NULL;
      struct ListNode* slow=head;
      struct ListNode* fast=head;
      while(fast && fast->next) {
      fast = fast->next->next;
      slow = slow->next;
      if (slow == fast) break;
      }
      if(!fast || !fast->next) return NULL;
      while (slow != head) {
      slow = slow->next;
      head = head->next;
      }
      return slow;
      }

[LeetCode] Linked List Cycle II, Solution的更多相关文章

  1. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  2. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

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

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

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

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

  5. [LeetCode]Linked List Cycle II解法学习

    问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...

  6. LeetCode——Linked List Cycle II

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

  7. [LeetCode] Linked List Cycle II 链表环起始位置

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

  8. Leetcode Linked List Cycle II

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

  9. LeetCode Linked List Cycle II 单链表环2 (找循环起点)

    题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...

随机推荐

  1. Dubbo学习源码总结系列四--集群容错机制

    Dubbo提供了哪些集群容错机制?如何实现的?         提供了六种集群容错机制,包括Failover(失败自动切换,尝试其他服务器).Failfast(失败立即抛出异常).Failsafe(失 ...

  2. Mysql配置信息

    MySQL配置信息 MySQl基本 由MySQL AB公司开发,隶属于Oracle公司 默认端口:3306 超级用户:root MySQL目录结构 两种安装方式 ZIP安装 MSI安装(仅Window ...

  3. 阿里P7前端需要哪些技能

    原谅我copy过来的,但是这个条理很清楚很有借鉴意义 前言 以下是从公众号的文章中获取到的一位阿里的前端架构师整理的前端架构p7的技能图谱,当然不是最完整.最系统的,所以之后我会一直维护更新这里的内容 ...

  4. Rsync+sersync部署

    内核版本:2.6.32-431.el6.x86_64 系统采用最小化安装,系统经过了基本优化,selinux 为关闭状态,iptables 为无限制模式 源码包存放位置:/root Rsync 客户端 ...

  5. 330-基于FMC接口的Kintex-7 XC7K325T PCIeX8 3U PXIe接口卡 光纤PCIe卡

    一.板卡概述      本板卡基于Xilinx公司的FPGAXC7K325T-2FFG900 芯片,pin_to_pin兼容FPGAXC7K410T-2FFG900 ,支持PCIeX8.64bit D ...

  6. java调用sqlldr报错:Message 2100 not found

    java调用Oracle的sqlldr命令报错:Message 2100 not found; No message file for product=RDBMS, facility=ULMessag ...

  7. 【学习】005 线程池原理分析&锁的深度化

    线程池 什么是线程池 Java中的线程池是运用场景最多的并发框架,几乎所有需要异步或并发执行任务的程序 都可以使用线程池.在开发过程中,合理地使用线程池能够带来3个好处. 第一:降低资源消耗.通过重复 ...

  8. springboot cache---本地缓存的使用

    使用缓存的几个注解 什么时候需要使用缓存呢?一般是在一个方法的返回值需要被频繁用到.但是返回值很少改变而且执行这个方法会消耗较多的时间,这种情况我们可以考虑将返回值暂时存到内存中,需要时通过对应的唯一 ...

  9. Redis使用场景一,查询出的数据保存到Redis中,下次查询的时候直接从Redis中拿到数据。不用和数据库进行交互。

    maven使用: <!--redis jar包--> <dependency> <groupId>redis.clients</groupId> < ...

  10. Day46(列表标签,表格标签,表单标签,css的引入方式,css选择器)

    一.列表标签 列表标签分为三种. 1.无序列表<ul>,无序列表中的每一项是<li> 英文单词解释如下: ul:unordered list,“无序列表”的意思. li:lis ...