• 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. vue 和react中子组件分别如何向父组件传值

    vue子组件和父组件双向传值: 子: Vue.component("childComponent",{ template:`<div><p @click='pos ...

  2. linux grep 设置高亮显示

    [root@eric ~]# vi /etc/profile alias grep='grep --color=auto' [root@eric ~]# source /etc/profile

  3. CentOS7系统局域网内配置本地yum源解决cannot find a valid baseurl for repo

    一.     问题详情 因为服务器无法连接外网,所有直接用yum安装某些功能将受到影响,报错如下: Error: Cannot find a valid baseurl for repo: base ...

  4. Taro -- 定义全局变量

    Taro定义全局变量 方法1:在taro中 getApp()只能取到一开始定义的值,并不能取到改变后的值 // app.js文件中 class App extends Component { cons ...

  5. RocketMQ集群部署安装

    RcoketMQ:[ 1.低延时:在高压下,1毫秒内超过99.6%的反应延迟. 2.面向金融:具有跟踪和审计功能的高可用性. 3.行业可持续发展:保证了万亿级的消息容量. 4.厂商中立:一个新的开放的 ...

  6. linux--基础知识2

    #超级用户root的家目录是/root ,而普通用户的家目录被存放在/home目录下 cd /目录  切换到指定目录 注意 /  是根目录 linux的一些重要目录 1.bin目录,用来存放常用的可执 ...

  7. 动态规划—distinct-subsequences

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  8. js实时计算价格

    //通过数量,单价的输入,实时显示总价 $("#number,#price").on("input",function(e){ $("#totalPr ...

  9. Tarjan 复习小结

    总算把这几个东西策清楚了. 在\(Tarjan\)算法里面,有两个时间戳非常重要,一个是\(dfn\),意为深度优先数,即代表访问顺序:一个是\(low\),意为通过反向边能到达的最小\(dfn\), ...

  10. bzoj4811 [Ynoi2017]由乃的OJ 树链剖分+贪心+二进制

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4811 题解 我现在为什么都写一题,调一天啊,马上真的退役不花一分钱了. 考虑这道题的弱化版 N ...