LeetCode 142. Linked List Cycle II 判断环入口的位置 C++/Java
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的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- (链表 双指针) 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 ...
- 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 ...
- 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 ...
- leetcode 142. Linked List Cycle II 环形链表 II
一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点 head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
随机推荐
- 网络编程三 Socket
1.根据netstat端口的找到进程号---->根据进程号找到进程名称-------->终止进程 1) netstat 最后一列是5432 C:\Users\Administrato ...
- 关于我与小组成员逐步升级C代码时的一些感想【第二次作业】
#include<stdio.h> #include<stdlib.h> #include <time.h> int main(){ srand(time(NULL ...
- 基于Linux-3.9.4的mykernel实验环境的极简内核分析
382 + 原创作品转载请注明出处 + https://github.com/mengning/linuxkernel/ 一.实验环境 win10 -> VMware -> Ubuntu1 ...
- 《python for data analysis》第八章,绘图与可视化
<利用python进行数据分析>一书的第8章,关于matplotlib库的使用,各小节的代码. # -*- coding:utf-8 -*-import numpy as npimport ...
- Android与Linux内核的对应关系
1. Android各版本和Linux版本对应关系 Android Version |API Level |Linux Kernel in AOSP ------------------------- ...
- mac上遇到的坑
mac上有两个python版本 当我执行命令时提示 command not found 但是我已经安装了包了 但是由于有两个python版本 当前项目用的时python3所以需要使用pip3 ...
- mysql 高版本only_full_group_by 错误
[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c ...
- C语言之二维数组与指针
二维数组: ][]; 二维数组实质上也就是一维数组,a[2][3]可看做一个2个元素的一维数组,这2个元素又是另一个数组,在内存中,它的排布如下: #include "stdio.h&quo ...
- Android OkHttp Get请求方式
1.导入okhttp-2.7.5.jar和okio-1.11.0.jar 2.Get请求 public void getDataByGet(){ OkHttpClient client = new O ...
- nginx 用来做什么?
代理服务端,反向代理,负载均衡. 其特点是占有内存少,并发能力强.