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?

Craking interview书上原题,快慢指针,话题较简单说明。

/**
* Definition for singly-linked list.
* class ListNode {
*    int val;
*    ListNode next;
*    ListNode(int x) {
*      val = x;
*      next = null;
*    }
* }
*/
public class Solution {
  public ListNode detectCycle(ListNode head) {
    // IMPORTANT: Please reset any member data you declared, as
    // the same Solution instance will be reused for each test case.
    ListNode slow = head;
    ListNode fast = head;
    while(fast != null && fast.next != null){
      slow = slow.next;
      fast = fast.next.next;
      if(slow == fast) break;
    }
    if(fast == null || fast.next == null) {
      return null;
    }
    slow = head;
    while(slow != fast){
      slow = slow.next;
      fast = fast.next;
    }
    return slow;
  }
}

转载于:https://www.cnblogs.com/shini/p/4436578.html

142. Linked List Cycle II相关推荐

  1. Leetcode 142. Linked List Cycle II

    地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...

  2. 【To Do】LeetCode 142. Linked List Cycle II

    LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...

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

  4. 【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 ...

  5. [LeetCode] 142. Linked List Cycle II

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

  6. Leetcode - 142. Linked List Cycle II

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

  7. 142. Linked List Cycle II 环形链表 II

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 - ...

  8. LeetCode 142. 环形链表 II(Linked List Cycle II)

    142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...

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

最新文章

  1. python真的这么厉害吗-嗯?python居然可以这么嚣张?这么厉害!到底是为什么?...
  2. linux工具之检测内存泄漏-valgrind
  3. 不可逆的类初始化过程
  4. 如何补救数据中心电缆
  5. Kubernetes 新玩法:在 yaml 中编程
  6. 使用 WordPress 插件模板开发高质量插件
  7. excel中match函数_Excel函数轻松学02:详解Excel函数中的数据类型
  8. 计算机usb共享网络泄密,杜绝USB泄密 MyUSBOnly
  9. 数字签名的java实现(RSA,DSA)
  10. 开箱即用 - jwt 无状态分布式授权
  11. 喷喷计算机语言掌握的程度
  12. Window 10 单机配置MYSQL主从同步
  13. 老兵不死:Radionomy正式宣布收购Winamp
  14. 计算机vfp系统,计算机等级考试VFP教程:第一章数据库系统
  15. 美团外卖用户差评情况分析
  16. 解决 linux下编译运行.sh文件报错 “[: XXXX: unexpected operator” 问题
  17. please select a vaild python interpret
  18. 计算机安装msvcr110.dll,帮您还原win7系统运行程序提示计算机中丢失msvcr110.dll的解决方法...
  19. pc端js根据图片url进行下载单张图片以及批量下载到本地
  20. PCB设计十条黄金法则

热门文章

  1. 接口自动化 基于python+Testlink+Jenkins实现的接口自动化测试框架
  2. SWF 文件不能访问本地资源 只有仅限于文件系统的 SWF 文件和可信的本地 SWF 文件可以访问本地资源。...
  3. ASP.NET存储Session的StateServer
  4. python字符串长度_如何使用python获取字符串长度?哪些方法?
  5. 安装后添加没有class library_《没有秘密的你》:戚薇曝光手机号,粉丝急忙添加后却甜哭了?...
  6. 多线程多进程解析:Python、os、sys、Queue、multiprocessing、threading
  7. 状态栏编程(显示系统时间和进度条)
  8. linux系统嵌入式编译环境,Ubuntu 12.04嵌入式交叉编译环境arm-linux-gcc搭建过程图解...
  9. 服务器物理内存总是九十几,服务器物理内存使用率90以上
  10. nginx 上传文件漏洞_文件上传及解析漏洞