1. public class Solution {
  2. public ListNode detectCycle( ListNode head ) {
  3. if( head == null || head.next == null ){
  4. return null;
  5. }
  6. // 快指针fp和慢指针sp,
  7. ListNode fp = head, sp = head;
  8. while( fp != null && fp.next != null){
  9. sp = sp.next;
  10. fp = fp.next.next;
  11. //此处应该用fp == sp ,而不能用fp.equals(sp) 因为链表为1 2 的时候容易
  12. //抛出异常
  13. if( fp == sp ){ //说明有环
  14. break;
  15. }
  16. }
  17. //System.out.println( fp.val + " "+ sp.val );
  18. if( fp == null || fp.next == null ){
  19. return null;
  20. }
  21. //说明有环,求环的起始节点
  22. sp = head;
  23. while( fp != sp ){
  24. sp = sp.next;
  25. fp = fp.next;
  26. }
  27. return sp;
  28. }
  29. }

补充一个python的实现:

  1. class Solution:
  2. def detectCycle(self, head: ListNode) -> ListNode:
  3. if head == None or head.next == None:
  4. return None
  5. slow,fast = head,head
  6. while fast != None and fast.next != None:
  7. slow = slow.next
  8. fast = fast.next.next
  9. if slow == fast:
  10. break
  11. if fast == None or fast.next == None:
  12. return None
  13. slow = head
  14. while slow != fast:
  15. slow = slow.next
  16. fast = fast.next
  17. return slow

leetcode142的更多相关文章

  1. 【算法训练营day4】LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表II

    [算法训练营day4]LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表 ...

  2. [Swift]LeetCode142. 环形链表 II | Linked List Cycle II

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

  3. LeetCode142:Linked List Cycle II

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

  4. Leetcode142 环形链表

    很多题解没有讲清楚非环部分的长度与相遇点到环起点那部分环之间为何是相等的这个数学关系.这里我就补充下为何他们是相等的.假设非环部分的长度是x,从环起点到相遇点的长度是y.环的长度是c.现在走的慢的那个 ...

  5. Leetcode142. Linked List Cycle II环形链表2

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶: 你是否可以不用额外空间解决此题? 方法一:使用map 方法二: 分两个步骤,首先通 ...

  6. LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

    链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

  7. leetcode142 Linked List Cycle II

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

  8. LeetCode142 环形链表 II

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? //章节 - 链表 //二.双指针技巧 //2.环 ...

  9. LeetCode 142

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

随机推荐

  1. Verilog强制激励语法

    Verilog强制激励语法 1. 在一个过程块中,可以用两种不同的方式对信号变量或表达式进行连续赋值. 过程连续赋值往往是不可以综合的,通常用在测试模块中. 两种方式都有各自配套的命令来停止赋值过程. ...

  2. Markup解析XML——文档,说明

    链接:http://pan.baidu.com/s/1slMwEc9 密码:slz7 上面是网盘的地址,因为来源已经找不到了,在这里给这个作者说声谢谢. 轻量级的XML解析器使用比较简单,下载Mark ...

  3. Jacobi-Anger expansion

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2017/06/16 适合于自己的关于Jacobi-Anger expansion的推导方法,这里记下来,方便以后查阅. ...

  4. noip-2006普及组-数列- 【模拟-找规律-快速幂】

    链接:https://ac.nowcoder.com/acm/contest/153/1047 来源:牛客网 题目描述 给定一个正整数k( ≤ k ≤ ),把所有k的方幂及所有有限个互不相等的k的方幂 ...

  5. 初识vue小结

    初识vue 大家都那么热爱他一定有原因,我也特想了解. 我来咯, 首先用vue开发版,用一个标签在head中插入,script标签src属性引入vue文件,就像jquey一样在script,但是放在h ...

  6. Opensource Licenses

    协议列表https://www.gnu.org/licenses/license-list.htmlhttps://opensource.org/licenses/alphabetical 协议选择参 ...

  7. vue实现淘宝购物车功能

    淘宝购物车功能,效果如下图 非常简单的逻辑,没有做代码的封装,代码如下 <div class="list-container"> <div class=" ...

  8. Cache架构设计

    Cache策略 定时过期策略 定时过期的好处是Cache节点的个数符合实际需求,不会造成资源滥用和服务器压力 定时过期适合访问量较大,实时性要求不高的情况 如果访问量小,定时过期会造成Cache命中率 ...

  9. Django学习笔记之数据库-QuerySet_API

    QuerySet API 我们通常做查询操作的时候,都是通过模型名字.objects的方式进行操作.其实模型名字.objects是一个django.db.models.manager.Manager对 ...

  10. [C#]时间格式和字符串的相互转换

    一.字符串转化为时间格式 string date = "2018/9/27 10:53:56"; DateTime dt1 = DateTime.Parse(date);//方式1 ...