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是为了更好的掌握它的用法,孰能生巧。😊

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

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

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

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

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

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

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

相关推荐
侯小啾5 分钟前
【23】C语言 左移(<<) 与 右移(>>) 位运算符在处理像素中的应用
c语言·算法·位运算·右移·左移
搂鱼11451414 分钟前
(dp 优化)洛谷 P14460 寻雾启示 题解
算法·图论
_OP_CHEN26 分钟前
算法基础篇:(十一)贪心算法拓展之区间问题:从重叠到覆盖的最优解艺术
算法·贪心算法
钟智强1 小时前
线性映射(Linear Mapping)原理详解:机器学习中的数学基石
人工智能·算法·机器学习
福尔摩斯张3 小时前
C语言核心:string函数族处理与递归实战
c语言·开发语言·数据结构·c++·算法·c#
2501_941884613 小时前
云计算与边缘计算:解锁未来计算架构的智能边界
leetcode
程序猿小白日记3 小时前
云计算与物联网融合:推动智慧城市的未来发展
leetcode
吗~喽3 小时前
【LeetCode】滑动窗口_水果成篮_C++
c++·算法·leetcode
立志成为大牛的小牛4 小时前
数据结构——四十九、B树的删除与插入
数据结构·学习·程序人生·考研·算法
高洁014 小时前
具身智能-普通LLM智能体与具身智能:从语言理解到自主行动 (2)
深度学习·算法·aigc·transformer·知识图谱