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

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

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

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

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

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

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

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

相关推荐
YuTaoShao7 分钟前
【LeetCode 热题 100】994. 腐烂的橘子——BFS
java·linux·算法·leetcode·宽度优先
Wendy14418 小时前
【线性回归(最小二乘法MSE)】——机器学习
算法·机器学习·线性回归
拾光拾趣录8 小时前
括号生成算法
前端·算法
渣呵9 小时前
求不重叠区间总和最大值
算法
拾光拾趣录9 小时前
链表合并:双指针与递归
前端·javascript·算法
好易学·数据结构9 小时前
可视化图解算法56:岛屿数量
数据结构·算法·leetcode·力扣·回溯·牛客网
香蕉可乐荷包蛋10 小时前
AI算法之图像识别与分类
人工智能·学习·算法
chuxinweihui10 小时前
stack,queue,priority_queue的模拟实现及常用接口
算法
tomato0910 小时前
河南萌新联赛2025第(一)场:河南工业大学(补题)
c++·算法
墨染点香11 小时前
LeetCode Hot100【5. 最长回文子串】
算法·leetcode·职场和发展