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

Note: Do not modify the linked list.

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

思路:设head距离循环开始点k,循环开始点距离fast和slow第一次相遇点x,slow还要走y到达循环开始点。则有:x+y+k=n;  n+x= 2* (k+n); 得到y=k。及相遇点到循环开始点的距离与head到循环开始点的距离相等,那么把slow放到head,fast和slow都用pace=1行走,则在循环开始点两者将相遇。

class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow = head;
ListNode* fast = head;
bool flag = false; while(fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if(slow == fast) {
slow = head;
flag = true;
break;
}
}
if(!flag) return NULL;
while(fast!=slow){
fast = fast->next;
slow = slow->next;
}
return fast;
}
};

142. Linked List Cycle II (List; Two-Pointers)的更多相关文章

  1. 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)

    题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...

  2. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

  3. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  4. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  5. Java for LeetCode 142 Linked List Cycle II

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

  6. 【LeetCode】142. Linked List Cycle II (2 solutions)

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

  7. [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 ...

  8. (链表 双指针) 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 ...

  9. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  10. [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 ...

随机推荐

  1. 【深度学习笔记】Anaconda及开发环境搭建

    在学习了一段时间台大李宏毅关于deep learning的课程,以及一些其他机器学习的书之后,终于打算开始动手进行一些实践了. 感觉保完研之后散养状态下,学习效率太低了,于是便想白天学习,晚上对白天学 ...

  2. PHP设置脚本最大执行时间的三种方法

    php.ini 中缺省的最长执行时间是 30 秒,这是由 php.ini 中的 max_execution_time 变量指定,如果脚本需要跑很长时间,例如要大量发送电子邮件,或者分析统计大量数据,服 ...

  3. 微信web端生成支付二维码

    授权获取二维码类: <?php /** * Trade类 * @author xyyphp * @date 2016/10/10 */ abstract class TradeControlle ...

  4. Mybatis数据的增删改查

    数据: Student{id int,name String ,age int} 配置mybatis-config.xml <?xml version="1.0" encod ...

  5. SpringMVC-Spring-Hibernate项目搭建之三-- freemarker & 静态资源整合

    一. 前段目录结构如下架构如下: 二. freemarker文件配置 在 web.xml 文件中指定 spring 配置文件的位置 三. 配置springmvc-servlet.xml文件 1)配置自 ...

  6. git clone 后使用子分支

    git clone 项目git地址 git branch -a 切换到子分支进行开发 git ckeckout 子分支名称,如:git checkout dev_feature_call git pu ...

  7. Git操作行

    基础层:-----------------#初始化一个版本仓库git init #复制远程版本库git clone url #添加远程版本库origingit remote add origin ur ...

  8. 使用wifi网卡笔记2----概念及工具iw(STA模式)

    1.认证和加密的概念 (1)概念 (2)阶段划分 初级版本:认证不需要密码, 传输不需要加密 认证不需要密码, 传输需要加密(用WEP算法) 认证需要密码(用WEP算法), 传输需要加密(用WEP算法 ...

  9. 2_python之路之多级菜单

    python之路之多级菜单 1.使用知识点 (1)列表,字典的使用 (2)if条件判断语句 (3)for/while循环的使用 2.代码详细 #!/usr/bin/env python # _*_ c ...

  10. Configuring Transitive IPMP on Solaris 11

    http://www.tokiwinter.com/configuring-transitive-ipmp-on-solaris-11/ We all know the pain of configu ...