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

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

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

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

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

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

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

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

相关推荐
不一样的故事1263 分钟前
高速采集箱
算法·信号处理
会员果汁31 分钟前
算法-拓扑排序-C
c语言·开发语言·算法
YGGP40 分钟前
【Golang】LeetCode 72. 编辑距离
算法·leetcode
YGGP1 小时前
【Golang】LeetCode 62. 不同路径
算法·leetcode
资生算法程序员_畅想家_剑魔1 小时前
算法-回溯-14
java·开发语言·算法
致Great1 小时前
大模型对齐核心技术:从第一性原理完整推导 PPO 算法!
人工智能·算法·大模型·agent·智能体
_w_z_j_1 小时前
二叉树的右视图(BFS或DFS)
算法·深度优先·宽度优先
cyyt1 小时前
深度学习周报(12.22~12.28)
人工智能·算法·机器学习
WBluuue1 小时前
Codeforces Global 31 Div1+2(ABCD)
c++·算法
会员果汁2 小时前
算法-并查集-C
c语言·开发语言·算法