力扣题解: 00141 & 00142

209 阅读3分钟
原文链接: mp.weixin.qq.com

00141 环形链表

题目描述

给定一个链表,判断链表中是否有环。

实例 1:

    输入:head = [3,2,0,-4], pos = 1

    输出:true

    解释:链表中有一个环,其尾部连接到第二个节点。

示例 2:

    输入:head = [1,2], pos = 0

    输出:true

    解释:链表中有一个环,其尾部连接到第一个节点。

示例 3:

    输入:head = [1], pos = -1

    输出:false

    解释:链表中没有环。

力扣地址

  • https://leetcode.com/problems/linked-list-cycle

  • https://leetcode-cn.com/problems/linked-list-cycle

解题报告

哈希表

本题解由微信公众号 小猿刷题提供, 错误之处, 欢迎指正.

通过检查一个结点此前是否被访问过来判断链表是否为环形链表。常用的方法是使用哈希表.

    /**

    * 微信公众号"小猿刷题"

    * Definition for singly-linked list.

    * class ListNode {

    * int val;

    * ListNode next;

    * ListNode(int x) {

    * val = x;

    * next = null;

    * }

    * }

    */

    public class Solution {

    public boolean hasCycle(ListNode head) {

    Set<ListNode> nodesSeen = new HashSet<>();

    while (head != null) {

    if (nodesSeen.contains(head)) {

    return true;

    } else {

    nodesSeen.add(head);

    }

    head = head.next;

    }

    return false;

    }

    }

双指针

本题解由微信公众号 小猿刷题提供, 错误之处, 欢迎指正.

定义两个不同速度的快、慢指针遍历链表,慢指针每次移动一步,而快指针每次移动两步。如果列表中不存在环,最终快指针将会最先到达尾部,此时我们可以返回 false

    /**

    * 微信公众号"小猿刷题"

    * Definition for singly-linked list.

    * class ListNode {

    * int val;

    * ListNode next;

    * ListNode(int x) {

    * val = x;

    * next = null;

    * }

    * }

    */

    public class Solution {

    public boolean hasCycle(ListNode head) {

    // 从链表头部开始

    ListNode slow = head;

    ListNode fast = head;

    while(fast != null && fast.next != null){

    // 快慢指针行走

    fast = fast.next.next;

    slow = slow.next;

    // 相遇说明有环

    if(slow == fast){

    return true;

    }

    }

    return false;

    }

    }

00142 环形链表 II

题目描述

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

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

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

示例 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

解释:链表中没有环。

进阶:

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

力扣地址

  • https://leetcode.com/problems/linked-list-cycle-ii/

  • https://leetcode-cn.com/problems/linked-list-cycle-ii/

解题报告

哈希表

本题解由微信公众号 小猿刷题提供, 错误之处, 欢迎指正.

如果我们用一个 Set 保存已经访问过的节点,我们可以遍历整个列表并返回第一个出现重复的节点

    /**

    * 微信公众号"小猿刷题"

    * 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) {

    Set<ListNode> visited = new HashSet<ListNode>();

    ListNode node = head;

    while (node != null) {

    if (visited.contains(node)) {

    return node;

    }

    visited.add(node);

    node = node.next;

    }

    return null;

    }

    }

双指针

本题解由微信公众号 小猿刷题提供, 错误之处, 欢迎指正.

图解快慢指针判断环&环入口算法.

    /**

    * 微信公众号"小猿刷题"

    * 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) {

    ListNode fast = head;

    ListNode slow = head;

    while(fast != null && fast.next != null){

    fast = fast.next.next; // 快指针移动2位

    slow = slow.next; // 慢指针移动1位

    // 快慢指针的处理只能判断是否有环,并不能判断出在哪里出现的环(相遇说明有环)

    if(fast == slow){

    fast = head;

    // 重置快指针, 然后各自都移动1位,再次相遇的地方就是环入口

    while(slow != fast){

    fast = fast.next;

    slow = slow.next;

    }

    return fast;

    }

    }

    return null;

    }

    }