LeetCode实战:环形链表 II

背景


题目英文

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.

Example 1:

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

Example 2:

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

Example 3:

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

Follow-up:

Can you solve it without using extra space?


题目中文

给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 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
解释:链表中没有环。

进阶:

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


算法实现

利用暴力匹配的方式

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode DetectCycle(ListNode head)
    {
        if (head == null)
            return null;

        ListNode p1 = head;
        int i = 0;
        while (p1 != null)
        {
            p1 = p1.next;
            i++;

            ListNode p2 = head;
            int j = 0;
            while (j < i)
            {
                if (p2 == p1)
                {
                    return p2;
                }
                p2 = p2.next;
                j++;
            }
        }
        return null;
    }
}

利用Hash的方式

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode DetectCycle(ListNode head)
    {
        if (head == null)
            return null;

        HashSet<ListNode> hash = new HashSet<ListNode>();
        hash.Add(head);

        ListNode temp = head.next;
        while (temp != null)
        {
            if (hash.Contains(temp))
                return temp;

            hash.Add(temp);
            temp = temp.next;
        }
        return null;
    }
}

实验结果

利用暴力匹配的方式

  • 状态:通过
  • 16 / 16 个通过测试用例
  • 执行用时: 412 ms, 在所有 C# 提交中击败了 17.07% 的用户
  • 内存消耗: 24.8 MB, 在所有 C# 提交中击败了 10.00% 的用户

提交结果

利用Hash的方式

  • 状态:通过
  • 16 / 16 个通过测试用例
  • 执行用时: 140 ms, 在所有 C# 提交中击败了 82.93% 的用户
  • 内存消耗: 26 MB, 在所有 C# 提交中击败了 5.00% 的用户

提交结果


相关图文

1. “数组”类算法

2. “链表”类算法

3. “栈”类算法

4. “队列”类算法

5. “递归”类算法

6. “字符串”类算法

7. “树”类算法

8. “哈希”类算法

9. “排序”类算法

10. “搜索”类算法

11. “动态规划”类算法

12. “回溯”类算法

13. “数值分析”类算法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青少年编程备考

感谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值