• 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. ln创建软链接方式

    ln -s 目标文件 软链接

  2. wordpress系统网站访问慢的解决方案

    从2013年5月底开始,google在中国基本处于无法访问状态,谷歌官网 域名,香港域名均无法访问,就连之前的IP访问方法也都失效,而Google Adsense打不开,恐怕做谷歌联盟的站长也要倒霉了 ...

  3. python去除字符串中间空格的方法

    1.使用字符串函数replace a = 'hello world' a.replace(' ', '') # 'helloworld' 2.使用字符串函数split a = ''.join(a.sp ...

  4. linux 调整内核优化

    所谓内核优化,主要是在 linux 中针对业务服务应用而进行的系统内核参数优化,优化并无特殊的 标准,下面以常见生产环境 linux 的内核优化为例讲解,仅供大家参考: 内核调优 #vi /etc/s ...

  5. 学Python的第八天

    最近因为很多生活琐事+生病+培训耽误了好几天的学习,不过幸好身体feel fly!! 今天依旧是爱Python的一天-.- 前几天以及今天所列出来的Python魔法类型不需要死记硬背熬.... #!/ ...

  6. linux使用v 2ray

    一.安装配置服务端程序 是时候使用 了,因为相对安全,使用方法很简单,使用root权限执行以下命令即可 $ sudo -i 一顿安装后如图 输入 命令可以查看链接,然后在客户端使用这个链接就能配置好了 ...

  7. P.W.N. CTF - Web - Login Sec

    题目 链接:https://ctftime.org/task/6934 题解 Login 1 题目给出了源码 var http = require('http'); const crypto = re ...

  8. 查看Linux系统所对应的版本

    #cat /etc/issue 在CentOS下执行显示为:CentOS release 5.7 (Final)Kernel \r on an \m 或在Ubuntu下显示为:Ubuntu 11.04 ...

  9. handy源码阅读(二):EventsImp类

    EventsImp用于完成事件的处理. class EventsImp { EventBase* base_; PollerBase* poller_; std::atomic<bool> ...

  10. 高级Javascript代码

    Javascript是一门很吊的语言,我可能学了假的JavaScript,哈哈,大家还有什么推荐的,补充送那啥邀请码. 本文秉承着:你看不懂是你SB,我写的代码就要牛逼. 1.单行写一个评级组件 &q ...