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

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos-1,则在该链表中没有环。

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

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.

示例 1:

输入:head = [3,2,0,-4], pos = 1
输出:tail connects to node index 1
解释:链表中有一个环,其尾部连接到第二个节点。

示例 2:

输入:head = [1,2], pos = 0
输出:tail connects to node index 0
解释:链表中有一个环,其尾部连接到第一个节点。

示例 3:

输入:head = [1], pos = -1
输出:no cycle
解释:链表中没有环。

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

Follow-up: Can you solve it without using extra space?

解题思路:

和上一道题比只多了一步判断入环节点在哪。两种方法:

哈希表:

哈希表添加节点时只要发现节点已经存在了,证明就有环形链表。并且已存在的节点即为入环节点

双指针:

画了个图帮助理解:

一快一慢双指针开始从头结点遍历链表,快节点速度为2,慢节点速度为1:

相遇时:

慢节点走了:a+b

由于快指针速度是慢指针的2倍,快节点走了:2(a+b)

快慢节点相遇时快节点比慢节点刚好多走了一圈环形节点。快节点走了:(a+b)+(b+c)

列方程:2(a+b)=(a+b)+(b+c)

解得 a=c

也就是说:相遇节点到入环节点的长度和头节点到入环节点的长度相等

可以得出结论,如果此时让慢节点或快节点中的一个指向头节点,另一个留在相遇节点,然后速度都为1,继续遍历链表,双指针再次相遇时的节点刚好是入环节点。

注:为了理解方便,把长度 b 定为上半部分长度,实际上 b 应该为快慢节点相遇时慢节点绕过环形链表的总长度

哈希表解题:

Java:

public class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null) return null;//如果是空链表直接返回
Set<ListNode> nodeSet = new HashSet<>();//构造哈希表
while (head.next != null) {//链表下一个不为空
if (nodeSet.contains(head)) return head;//哈希表包含该节点则存在环形链表
nodeSet.add(head);//加入节点
head = head.next;//下移一位
}
return null;
}
}

Python:

class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head is None:
return None
hashSet=set()#构造集合
while(head.next is not None):
if head in hashSet:#是否已存在
return head
hashSet.add(head)
head=head.next
return None

双指针解题:

Java:

public class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null || head.next == null) {
return null;
}
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {//快指针及其下一位是否为null
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {//如果相同,存在环形链表
slow = head;//指向头节点
while (slow != fast) {//继续遍历,再次相遇时的节点即为入环节点
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
return null;
}
}

Python:

class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head is None or head.next is None:
return None
slow, fast = head, head
while fast is not None and fast.next is not None:
slow, fast = slow.next, fast.next.next
if slow == fast:
slow = head
while slow != fast:
slow = slow.next
fast = fast.next
return slow
return None

欢迎关注公众号:爱写Bug(ID:iCodeBugs)

LeetCode 142:环形链表 II Linked List Cycle II的更多相关文章

  1. LeetCode 141. 环形链表(Linked List Cycle)

    题目描述 给定一个链表,判断链表中是否有环. 进阶:你能否不使用额外空间解决此题? 解题思路 快慢指针,慢指针一次走一步,快指针一次走两步,若两者相遇则说明有环,快指针无路可走则说明无环. 代码 /* ...

  2. LeetCode 142. 环形链表 II(Linked List Cycle II)

    142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...

  3. Java实现 LeetCode 142 环形链表 II(二)

    142. 环形链表 II 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始 ...

  4. Leetcode 142.环形链表II

    环形链表II 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? 链表头是X,环的第一个节点是Y,sl ...

  5. 【Leetcode】【Medium】Linked List Cycle II

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

  6. LeetCode 142. 环形链表 II(Linker List Cycle II)

    题目描述 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶: 你是否可以不用额外空间解决此题? 解题思路 分为三步: 首先判断是否存在 ...

  7. [Swift]LeetCode142. 环形链表 II | Linked List Cycle II

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

  8. LeetCode 142——环形链表II(JAVA)

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 - ...

  9. leetcode 142. 环形链表 II(c++)

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 - ...

随机推荐

  1. pycharm问题解析(connecting to console)

    1. 场景描述 以前一直用的anaconda3,临时下载了demo用的python2,就下载anaconda2安装了下,测试过后,发现pycharm中以前的项目跑不起来了,一直报:connecting ...

  2. vue中template的作用及使用

     先来看一个需求:下图div用v-for做了列表循环,现在想要span也一起循环,应该怎么做? 有3种方法可以实现 ①:直接用v-for对span也循环一次(该方法虽然可以使用,但不要用这种方式,因为 ...

  3. CRF 详细推导、验证实例

    逐帧softmax CRF主要用于序列标注问题,可以简单理解为是给序列中的每一帧都进行分类,既然是分类,很自然想到将这个序列用CNN或者RNN进行编码后,接一个全连接层用softmax激活,如下图所示 ...

  4. MECE分析法

      概述 MECE分析法,是麦肯锡的第一个女咨询顾问 Barbara Minto 在金字塔原理中提出的一个很重要的原则. MECE分析法,全称Mutually Exclusive Collective ...

  5. ASH裸数据dba_hist_active_sess_history的分析

    之前在一则案例<记录一则enq: TX - row lock contention的分析过程>使用过这种方法. 因为最近故障处理经常会用到这类查询进行ASH裸数据的分析,下面以m_ash0 ...

  6. 【IDEA】(1)---MAC下常用快捷键

    IDEA常用快捷键 IDEA是一个很好的开发工具,用好它能大大提高我们的开发效率,所以这里学习总结下有关IDEA实用的一些教程,比如常用快捷键,如何自定义代码模版,如何debug异常断点,或者说多线程 ...

  7. 黑马程序员面试宝典(Java)Beta6.0免费下载

    场景 JavaSE基础 面向对象特征以及理解 访问权限修饰符区别 理解clone对象 JavaSE语法 java有没有goto语句 &和&&的区别 如何跳出当前的多重嵌套循环? ...

  8. xml解析-jaxp修改结点

    jaxp修改结点 / 修改第一个p1下面的sex内容是nan * 1.创建解析器工厂 * 2.根据解析器工厂创建解析器 * 3.解析xml返回document * 4.得到sex item方法 * 5 ...

  9. 开关VoLTE流程分析(一)

    开关按钮位置: 设置--> 更多--> 移动网络--> 增强型4G LTE模式 控件初始化addEnhanced4GLteSwitchPreference,该设置开关使用了Switc ...

  10. java annotation使用介绍

    还望支持个人博客站:http://www.enjoytoday.cn 介绍 Annotation的中文名字叫注解,开始与JDK 1.5,为了增强xml元数据和代码的耦合性的产物.注解本身并没有业务逻辑 ...