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. 物体检测中常用的几个概念迁移学习、IOU、NMS理解

    1.迁移学习 迁移学习也即所谓的有监督预训练(Supervised pre-training),我们通常把它称之为迁移学习.比如你已经有一大堆标注好的人脸年龄分类的图片数据,训练了一个CNN,用于人脸 ...

  2. poj2243前一道题升级(思维构造+ac自动机)

    题:http://acm.hdu.edu.cn/showproblem.php?pid=2243 题意:给出m个模式串,求长度小于n的且存在模式串的字符串数有多少个(a~z) 分析:我们反着来,用总的 ...

  3. 蓝桥杯2015-省赛-C/C++-A组2题 星系炸弹

    在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标.每个炸弹都可以设定多少天之后爆炸.比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸.有一个贝塔 ...

  4. 如何编写一个vue应用

    1.vue应用的组成 1.1 vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化 通常 ...

  5. linux服务器CentOS7安装node.js

    方维一元夺宝2.0版本,很多用户面临机器人自动执行任务.采集计划一直无法开启的问题. 这个需要开启node.js分享给大家. 1.获取node.js资源 V5.x: curl --silent --l ...

  6. Tooltips2

    #include<windows.h> #include<Commctrl.h> #include"resource.h" #pragma comment( ...

  7. JavaScript之OOP

    本文介绍下js中OOP的一些用法: 由上图可得: 1.typeof null结果是object,所以需要用与运算符再次判断是否为空. 2.构造器实现重载后,可依序传入参数或传入对象. 由上图可得:要实 ...

  8. QLIKVIEW-SALESORDER\DELIVERYNOTICE\OUTSTOCK\INVOICE

    //销售订单SALESORDER_TMP:NoConcatenateLOAD T_SAL_ORDER.LE_ID, [T_SAL_ORDER.LCY CODE], T_SAL_ORDER.SYSTEM ...

  9. python 并发执行

    并发执行, 精简代码. 适用python2 和python3 # -*- encoding:utf-8 -*- from threading import Thread from multiproce ...

  10. F - Fraction Formula Gym - 102307F

    Mr. Potato Head has been promoted and now is a math professor at the UNAL. For his first course he i ...