题目链接

题目大意:141题目的扩展,给出单链表,判断是否有环,如果有环,找出环的开始的结点,如果没有环,返回null。

法一(借鉴):在已经找出单链表环的基础上再找开始结点,要时刻记住这个环不一定是从尾结点到头结点,有可能是中间的某一段。所以对于这里具体路径的分析,有两个博客可看http://blog.csdn.net/xy010902100449/article/details/48995255http://blog.csdn.net/willduan1/article/details/50938210,都是从数学层面去证明方法的可行性,也就是为什么可以在找到相遇点后,再从头开始遍历,第二次相遇点就是开始结点的问题。代码如下(耗时1ms):

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

法二:利用了141的法二,直接用set存入,如果有相同的则说明肯定是环的开始结点,直接返回即可。此方法的时间复杂度和空间复杂度都挺大的。代码如下(耗时16ms):

         Set<ListNode> list = new HashSet<>();
while(head != null) {
if(list.contains(head)) {
return head;
}
else {
list.add(head);
head = head.next;
}
}
return null;

142.Linked List Cycle II---双指针的更多相关文章

  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 链表中的环 II

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

  10. 【LeetCode】142. Linked List Cycle II

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

随机推荐

  1. CF708C-Centroids

    题目 一棵树的重心定义为一个点满足删除这个点后最大的连通块大小小于等于原来这颗树大小的一半. 给出一棵树,一次操作为删除一条边再添加一条边,操作结束后必须仍为一棵树.问这颗树的每个点是否可以通过一次操 ...

  2. [BZOJ3166][Heoi2013]Alo 可持久化Trie树

    3166: [Heoi2013]Alo Time Limit: 20 Sec Memory Limit: 256 MB DescriptionWelcome to ALO ( Arithmetic a ...

  3. 【转】Unable to load embedded resource from assembly 无法加载的程序集嵌入的资源

    转自:http://blog.sina.com.cn/s/blog_994678b90101f035.html 项目运用IbatisNet 今天更新项目,编译完点击运行,报错如下: [“/”应用程序中 ...

  4. Spring Boot系列教程七:Spring boot集成MyBatis

    一.创建项目         项目名称为 “springboot_mybatis_demo”,创建过程中勾选 “Web”,“MyBatis”,“MySQL”,第一次创建Maven需要下载依赖包(耐心等 ...

  5. 【BZOJ2727】双十字(动态规划,树状数组)

    [BZOJ2727]双十字(动态规划,树状数组) 题面 BZOJ 洛谷 题解 我们去年暑假的时候考试考过. 我当时写了个大暴力混了\(70\)分.... 大暴力是这么写的: 预处理每个位置向左右/上/ ...

  6. innodb--表空间

    MySQL把数据库中表结构的定义信息保存到数据库目录的.frm文件中. 在InnoDB中数据库中存储的数据及索引实际是存放在表空间里的(tablespace). 可以将每个基于InnoDB存储引擎的表 ...

  7. 洛谷 P1486 BZOJ 1503 NOI 2004 郁闷的出纳员 fhq treap

    思路: 1. 此处的fhq treap的分裂是按照权值分裂然后插入的.将小于k的分为一棵子树,大于等于k的分为另一棵子树. 2. 删除的时候只要将大于等于min的分裂到以root为根的树中,另一部分不 ...

  8. 【bzoj3672】购票

    Portal -->bzoj3672 Solution 天知道我是怎么调完的qwq调到天昏地暗系列.. ​ 不管这么多,先尝试列一个最简单的状态转移方程 用\(f[i]\)表示\(i\)点到\( ...

  9. ios错误码:NSError对象.code

    1. URL Loading System Error Codes These values are returned as the error code property of an NSError ...

  10. jetBrains 插件开发第一课-- 在主菜单栏新增一个菜单

    环境搭建完了,接下来可以开始写代码了: 1.新建 plugin 项目 2.编辑 plugin.xml,修改一下里面的插件名那些信息,该文件的配置项可以看这里:plugin.xml 其中比较关键的有一个 ...