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

Example 1:

Input: head = [,,,-], pos =
Output: tail connects to node index
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

Input: head = [,], pos =
Output: tail connects to node index
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:

Input: head = [], pos = -
Output: no cycle
Explanation: There is no cycle in the linked list.

这题解题的思路在于:在第一次相遇点位置pos,从该位置到环入口Join的距离=从头结点Head到环入口Join的距离

假设环的长度是r,在第一次相遇时,慢指针走过的路程:s=lenA+x,快指针走过的路程:2s=lenA+nr+x,所以:lenA+x=nr,即:LenA=nr-x。

所以在第一次相遇之后,一个指针从head走到join的路程,另一个指针从pos走到join。

方法一(C++)

 ListNode *detectCycle(ListNode *head) {
ListNode* slow=head,*fast=head;
while(fast&&fast->next){
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
break;
}
if(!fast||!fast->next)
return NULL;
slow=head;
while(slow!=fast){
slow=slow->next;
fast=fast->next;
}
return slow;
}

(java):

 ListNode slow=head,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;
}

LeetCode 142. Linked List Cycle II 判断环入口的位置 C++/Java的更多相关文章

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

  2. [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 ...

  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. To r ...

  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. To r ...

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

  6. leetcode 142. Linked List Cycle II ----- java

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

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

    一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点  head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...

  8. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  9. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

随机推荐

  1. a链接中 JS弹出确认对话框方法

    一种: <a href="javascript:if(confirm('确实要删除该内容吗?'))location='http://www.google.com'">弹 ...

  2. for循环的运算 改变循环的控制流 死循环 遍历数组 定义方法 有名函数匿名函数 定义函数的方法取值 与 自己创建函数取值 局部与全局变量 次幂/随机数/取绝对值/向上取整/平方根

    今天学习的是for循环,对for循环的运算有了理解. document.write(" ")里的内容在网页上展示出来 有名函数非常重要!!!!!!!!!!!!!!!!!!!!!并且 ...

  3. Pagedown learning notes

    Pagedown Links Google wiki page Download Markdown.Converter.js var converter = new Markdown.Converte ...

  4. [Python学习笔记] turtle库的基本使用

    turtle库常用函数 引入turtle模块 import turtle turtle的绘图窗体 #setup()设置窗口大小及位置#setup()可省略turtle.setup(width,heig ...

  5. 【mysql-server】遇到的坑

    一.前提 最近因为工作原因,不得不用windows电脑,发现windows装mysql真的坑太多 23333: 二.环境 windows 环境 mysql 5.7(不建议用5.8坑更多) 三.步骤 3 ...

  6. Centos7快速部署CloudStack服务器

    OS:centos7 server:172.16.13.159 client:172.16.13.156 1.下载要用到的安装包 在client上: wget http://download.clou ...

  7. Python 反射(reflection)

    反射是指通过字符串映射或修改程序运行时的状态.属性.方法, 有以下4个方法 1.getattr(object, name, default = None) 根据字符串获取 obj 对象里对应 str ...

  8. 【爬虫综合作业】猫眼电影TOP100分析

    作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3075 一.爬虫对象 猫眼电影TOP100排行榜 二.代码如下 im ...

  9. Jquery 一个页面多个倒计时 实现

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. php使用insert语句动态添加用户

    <html> <head> <title>Adding User</title> </head> <body> <h2&g ...