• 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. ASE Code Search

    重现基线模型 Hamel's model 基线模型原理 如何实现semantic search?在已有数据库的基础上,衡量一个句子和每段代码的相关性再进行排序,选出最优代码片段即可实现一个通用的cod ...

  2. 总结const、readonly、static三者的区别【收藏、转载】20190614

    总结const.readonly.static三者的区别 const:静态常量,也称编译时常量(compile-time constants),属于类型级,通过类名直接访问,被所有对象共享! a.叫编 ...

  3. popen, pclose - process I/O

    SYNOPSIS #include <stdio.h> FILE *popen(const char *command, const char *type); int pclose(FIL ...

  4. 07.SUSE Linux 系统本地yum源配置

    SUSE Linux 系统 1.新建本地源存储目录root@suse:mkdir /mnt/SUSE_LOCAL_SOURCE 2.创建zypper本地源root@suse:zypper ar fil ...

  5. linux--基础知识4

    #当前已什么用户登陆,创建的目录或文件,他的属组和主就是谁 #用户对目录拥有的几种权限 # ll -d查看目录当前权限信息 #r:可以查看该目录下的子文件名,子目录 #w:可以在该目录下创建,删除,重 ...

  6. CreateProcessAsUser 服务调用

    CreateProcessAsUser 函数 如果想通过服务向桌面用户Session 创建一个复杂UI 程序界面,则需要使用CreateProcessAsUser 函数为用户创建一个新进程用来运行相应 ...

  7. Android App渗透测试工具汇总

    网上搜集了一些App安全学习教程及工具,项目地址:https://github.com/Brucetg/App_Security 一. drozer简介 drozer(以前称为Mercury)是一款A ...

  8. CSS多列布局(栅格布局)

    一.多列布局 CSS3 可以将文本内容设计成像报纸一样的多列布局,如下实例: 代码如下(具体的解释也在代码中)浏览器支持表格中的数字表示支持该方法的第一个浏览器的版本号. 紧跟在数字后面的 -webk ...

  9. struts2+ajax 前后端传值

    摘要: 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的数据,并转换为json类型模式数据 3.配置struts.xml文件 4.页面脚本接受并处理数 ...

  10. (arm板子tensorflow安装)armv7板子pip安装的wheel

    树莓派之类的armv7板子在,安装 numpy,scipy时经常失败,因为安装过程是下载源码包到本地编译,然后再安装的,编译过程中往往就会失败. https://www.piwheels.org/si ...