给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

说明:不允许修改给定的链表。

进阶:

你是否可以不用额外空间解决此题?

方法一:使用map

方法二:

分两个步骤,首先通过快慢指针的方法判断链表是否有环;如果有环,则寻找入环的第一个节点。具体的方法为,首先假定链表起点到入环的第一个节点A的长度为a,到快慢指针相遇的节点B的长度为(a + b)。现在我们想知道a的值,注意到快指针p2始终是慢指针p走过长度的2倍,所以慢指针p从B继续走(a + b)又能回到B点,如果只走a个长度就能回到节点A。但是a的值是不知道的,注意到起点到A的长度是a,那么可以用一个从起点开始的新指针q和从节点B开始的慢指针p同步走,相遇的地方必然是入环的第一个节点A。

class Solution {
public:
ListNode *detectCycle(ListNode *head)
{
if(head == NULL)
return NULL;
ListNode *fast = head;
ListNode *slow = head;
bool haveCycle = false;
while(slow ->next != NULL && fast ->next != NULL && fast ->next ->next != NULL)
{
slow = slow ->next;
fast = fast ->next ->next;
if(slow == fast)
{
haveCycle = true;
break;
}
}
if(haveCycle)
{
ListNode *node = head;
while(node != slow)
{
node = node ->next;
slow = slow ->next;
}
return node;
}
else
return NULL;
}
};

Leetcode142. Linked List Cycle II环形链表2的更多相关文章

  1. 【LeetCode】Linked List Cycle II(环形链表 II)

    这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? ...

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

    给一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null.说明:不应修改给定的链表.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/proble ...

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

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

  4. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  5. 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 y ...

  6. [LeetCode] Linked List Cycle II 单链表中的环之二

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

  7. [Leetcode] Linked list cycle ii 判断链表是否有环

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

  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. [LC]141题 Linked List Cycle (环形链表)(链表)

    ①中文题目 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 示例 ...

随机推荐

  1. MybatisPlus联合分页查询

    跟单表分页查询差不多 1.编写查询语句 public interface QuestionMapper extends BaseMapper<Question> { @Select(&qu ...

  2. java-day09

    接口 就是一种公共规范标准,只要符合规范标准,就可以大家通用,多个类的公告规范,引用数据类型 格式 public interface 接口名称{} 接口都能定义抽象方法 public abstract ...

  3. iOS逆向系列-脱壳

    概述 通过iOS逆向系列-逆向App中使用class-dump工具导出App的Mach-O文件所有头文件.Hopper工具分析App的Mach-O文件代码大概实现.但是这些前体是App的Mach-O没 ...

  4. 记mysql 启动不了了的解决方法

    系统: centos7 本地的环境,mysql启动不了,查看 /var/log/mysqld.log 有以下内容 2018-12-24T08:05:38.090527Z 0 [Warning] TIM ...

  5. 二分图——poj2239

    水题 /* n门课,每门课有一个时间t 要求最大的n->t的匹配 */ #include<iostream> #include<cstring> #include< ...

  6. vagrant网站中box下载方法

    假设需要下载Laravel/homestead这个包. 首先定位到地址:https://app.vagrantup.com/laravel/boxes/homestead/versions/8.0.0 ...

  7. Python高质量缩放切图,抗锯齿

    最近刚接触Python,以迅雷不及掩耳盗铃之势(只是迫不及待)应用到工作中去了之前用 cmd+photoshop做批量图像处理(缩放切片),在执行效率(速度)上和灵活度上有很大限制,遂转战Python ...

  8. CPU中的主要的寄存器

    寄存器 名为寄存器的存储电路. 8种16位寄存器 AX accumulator 累加寄存器 CX counter 计数寄存器 DX data 数据寄存器 BX base 基址寄存器 SP stack ...

  9. PAT甲级——A1086 Tree Traversals Again

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...

  10. Python学习day24-面向对象的三大特征之继承

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...