Question:

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

Analysis:

只想到了,首先判断是否有环,若有环,则总链首开始,一次判断是否是环的开始,这样T(O) = O(n^2)。

其实是一个数学问题,详细思路参照链接http://blog.csdn.net/sbitswc/article/details/27584037 。

Answer:

    public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;
while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if(fast == slow)
break;
} if(fast == null || fast.next == null)
return null; slow = head;
while(fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}

LeetCode -- Linked List Circle ii的更多相关文章

  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, return nu ...

  5. LeetCode——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 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, returnnull. Follo ...

  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, Solution

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

随机推荐

  1. javaWeb项目加载HTML文件时报错 [No Find /index.html]

    直接上主题: 在web.xml文件中添加如下信息: <display-name>Spring MVC Application</display-name> <servle ...

  2. CentOS下删除MySql

    1.查找以前是否装有mysql rpm -qa | grep -i mysql 显示之前安装了: MySQL-client-5.5.49-1.linux2.6.i386 MySQL-server-5. ...

  3. 搭建ssm框架经验

    要想搭建好ssm框架的开发环境.首先我们需要知道ssm是什么?ssm就是spring,springmvc,mybatis. 然后我们要知道,他们分别都是怎样实现的和做什么用的,充当什么角色?这样我们就 ...

  4. Kubernetes-深入分析集群安全机制(3.6)

    集群的安全性主要考虑以下几个方面: 容器与所在宿主机的隔离: 限制容器给基础设施及其他容器带来消极影响的能力: 最小权限原则--合理限制所有组件的权限,确保组件只执行它被授权的行为,通过限制单个组件的 ...

  5. PHP.42-TP框架商城应用实例-后台17-商品属性3-商品分类的修改与删除

    商品分类的修改 1.改表单Goods/edit.html,加下拉框 2.因为商品属性修改涉及商品属性表goods_attr{id,attr_value,attr_id,goods_id}与属性表att ...

  6. 【Consul】Consul架构-Gossip协议

    Consul使用gossip协议管理成员关系.广播消息到整个集群.详情可参考Serf library,Serf使用到的gossip协议可以参阅"SWIM: Scalable Weakly-c ...

  7. Android log 里面快速搜索错误堆栈 ( 关键字)

    有时候,别人给你的log 文件,是一个文件夹,里面放了很多文件.但是可能你需要的log 只有几行.这时候不可能手工搜索的. 那怎么办呢?使用FileLocationPro.下载地址: https:// ...

  8. mac 下 安装php扩展 - mcrypt

    由于自带的libmcrypt 可能版本低 另外通过brew安装的也不管用得去下载libmcrypt后编译安装 tar zxvf libmcrypt-2.5.8.tar.gz cd libmcrypt- ...

  9. web前端/移动端H5博客专家博客大全--值得收藏的前端技术大牛博客地址

    web前端/移动端H5博客专家博客大全--值得收藏的前端技术大牛博客地址   Huang Jie Blog .Com-前端开发 http://www.huangjieblog.com/?feed=rs ...

  10. php用GD库给图片添加水印

    php用GD库给图片添加文字水印,整个代码比较简单,DEMO如下: <?php /*打开图片*/ //1.配置图片路径 $src = "aeroplane.jpg"; //2 ...