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

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

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

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

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

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

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

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

相关推荐
方案开发PCBA抄板芯片解密13 小时前
什么是算法:高效解决问题的逻辑框架
算法
songx_9913 小时前
leetcode9(跳跃游戏)
数据结构·算法·游戏
学c语言的枫子14 小时前
数据结构——双向链表
c语言·数据结构·链表
小白狮ww14 小时前
RStudio 教程:以抑郁量表测评数据分析为例
人工智能·算法·机器学习
AAA修煤气灶刘哥14 小时前
接口又被冲崩了?Sentinel 这 4 种限流算法,帮你守住后端『流量安全阀』
后端·算法·spring cloud
kk”15 小时前
C语言快速排序
数据结构·算法·排序算法
纪元A梦15 小时前
贪心算法应用:基因编辑靶点选择问题详解
算法·贪心算法
3壹15 小时前
数据结构精讲:栈与队列实战指南
c语言·开发语言·数据结构·c++·算法
skytier16 小时前
Construct内报错和定位解决
算法
skytier16 小时前
Ascend print数据落盘使用
算法