day20【LeetCode力扣】142.环形链表Ⅱ

day20【LeetCode力扣】142.环形链表Ⅱ

1.题目描述

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

如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始 )。如果 pos-1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。

不允许修改 链表。

示例 1:

复制代码
输入:head = [3,2,0,-4], pos = 1
输出:返回索引为 1 的链表节点
解释:链表中有一个环,其尾部连接到第二个节点。

示例 2:

复制代码
输入:head = [1,2], pos = 0
输出:返回索引为 0 的链表节点
解释:链表中有一个环,其尾部连接到第一个节点。

示例 3:

复制代码
输入:head = [1], pos = -1
输出:返回 null
解释:链表中没有环。

2.题解

双指针法,用来判断是否存在环。

c++

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode* fast=head;
        ListNode* slow=head;
        while(fast&&fast->next){
            fast=fast->next->next;
            slow=slow->next;
            if(slow==fast){
            ListNode* slow2=head;
            while(fast!=slow2){
                fast=fast->next;
                slow2=slow2->next;
            }
            return slow2;
            }
        }
        return NULL;
    }
};

python

python 复制代码
class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        fast=head
        slow=head
        while fast and fast.next:
            fast=fast.next.next
            slow=slow.next
            if fast==slow:
                slow2=head
                while fast!=slow2:
                    slow2=slow2.next
                    fast=fast.next
                return slow2
        return None

哈希法visited 哈希集合的作用可能是用于记录访问过的链表节点的地址,以防止链表中出现环形结构。这种情况通常是在判断链表是否有环时会用到,具体的做法是遍历链表,遍历每个节点时将该节点的地址存入 visited 中,对于每个节点,都先检查它是否存在于 visited 中,如果存在,则说明链表中存在环。

cpp 复制代码
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        unordered_set<ListNode *>visited;
        while(head!=nullptr){
            if(visited.count(head))
                return head;
            visited.insert(head);
            head=head->next;
        }
        return NULL;
    }
};

写法2:

cpp 复制代码
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        unordered_set<ListNode *> visited;
        while (head != nullptr) {
            if (visited.find(head) != visited.end()) {
            return head; // 链表中存在环
            }
            visited.insert(head);
            head = head->next;
        }
        return NULL; // 链表不包含环
    }
};

python

python中是结合着集合。

python 复制代码
class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        visited=set()
        while head:
            if head in visited:
                return head
            visited.add(head)
            head=head.next
        return None

每次写的都是c++和python两种语言的写法,c++是为了练习算法思想,而python是为了更好的掌握它的用法,孰能生巧。😊

链表篇章已完结,接下来让我们走进哈希算法中!!

​ ++如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!++

​ ++如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!++

​ ++如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!++

​ ++如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!++

​ ++如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!++

​ ++如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!++

相关推荐
IT从业者张某某8 分钟前
机器学习-04-分类算法-03KNN算法案例
算法·机器学习·分类
chen_song_11 分钟前
WebRTC的ICE之TURN协议的交互流程中继转发Relay媒体数据的turnserver的测试
算法·音视频·webrtc·交互·媒体
蒙奇D索大27 分钟前
【数据结构】图解图论:度、路径、连通性,五大概念一网打尽
数据结构·考研·算法·图论·改行学it
uhakadotcom31 分钟前
2025年春招:如何使用DeepSeek + 豆包优化简历,轻松敲开心仪公司的大门
算法·面试·github
小白狮ww35 分钟前
Retinex 算法 + MATLAB 软件,高效率完成图像去雾处理
开发语言·人工智能·算法·matlab·自然语言处理·图像识别·去雾处理
trust Tomorrow2 小时前
每日一题-力扣-2278. 字母在字符串中的百分比 0331
算法·leetcode
Lecea_L2 小时前
你能在K步内赚最多的钱吗?用Java解锁最大路径收益算法(含AI场景分析)
java·人工智能·算法
Tony882 小时前
热题100 - 394. 字符串解码
java·算法
Lecea_L2 小时前
🔍 找到数组里的“节奏感”:最长等差子序列
java·算法
是Dream呀2 小时前
ResNeXt: 通过聚合残差变换增强深度神经网络
人工智能·算法