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

To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

Note: Do not modify the linked list.

/**
* 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) {
if (head == null || head.next == null) {
return null;
}
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
// get the entry point when cur and slow meet
ListNode cur = head;
while(cur != slow) {
cur = cur.next;
slow = slow.next;
}
return slow;
}
}
return null;
}
}

[LC] 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

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

随机推荐

  1. 2019.11.18CTFD搭建记录

    ### 0x01 实验室纳新,准备在自己服务器搭建个ctfd给新生们玩玩,忙活了一天orz[大一刚开学就搭建过没这么费力啊..] 现在大二了没想到能折腾一天... 直接说下我踩的坑吧,给后来的人们说说 ...

  2. Python D9 学习

    Python 设置环境 当安装好Python 后 在计算机的属性里面   高级语言设置  环境变量. 环境变量里面的path  更改为Python的 树目录  可以从计算机直接下达命令 打开Pytho ...

  3. 选择排序&冒泡排序

    //直接选择排序 #include<stdio.h> void SelectionSort(int arr[],int len) { int i,j; int k,min; int tem ...

  4. spring学习之spring入门

    一 spring的基础 1:什么是spring spring是由Rod Johnson组织和开发的一个分层 的Java SE/EE 一站式轻量级开源框架,它以Ioc(控制反转)和 AOP(面向切面编程 ...

  5. 67)vector的begin() end() 和 front() back()的区别 rbegin() rend()

    1) ·············· 2)`````````v1.begin() 和v1.end()  是作为迭代器v1的 第一个位置  和 最后一个元素的下一个位置. `````````````v1. ...

  6. 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制

    spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...

  7. DB---WAL技术

    摘要:这个WAL技术也需要加以整理,即 write-ahead logging (预写式日志),待整理...

  8. UML-SSD总结

    1.不是所有场景都需要画SSD.需要画SSD的场景: 1).主成功场景 2).频繁发生的场景 3).复杂的场景 2.角色 1).参与者 2).系统(没有类,即黑盒) 3.画SSD时间不要过长,一般几分 ...

  9. dubbo的灰度发布

    1,什么是灰度发布 当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用. 可以按照以下的步骤进行版本迁移: 在低压力时间段,先升级一半提供者为新版本 再将所有消费者升级为 ...

  10. 【YOLO学习】召回率(Recall),精确率(Precision),平均正确率(Average_precision(AP) ),交除并(Intersection-over-Union(IoU))

    摘要 在训练YOLO v2的过程中,系统会显示出一些评价训练效果的值,如Recall,IoU等等.为了怕以后忘了,现在把自己对这几种度量方式的理解记录一下. 这一文章首先假设一个测试集,然后围绕这一测 ...