力扣:LCR 022. 环形链表 II

15 篇文章 0 订阅

力扣:LCR 022. 环形链表 II
给定一个链表,返回链表开始入环的第一个节点。 从链表的头节点开始沿着 next 指针进入环的第一个节点为环的入口节点。如果链表无环,则返回 null。

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。
在这里插入图片描述

解法一

快慢指针检测到环后,将慢重新指向头,在将两个指针一步一步前进,二者再次相遇处便是环的入口
原理为:
在这里插入图片描述

#include <iostream>
using namespace std;

// 定义链表节点结构体
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};

// 检测链表中是否有环,并返回环的入口节点
ListNode* detectCycle(ListNode* head) {
    ListNode* slow = head;
    ListNode* fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
        if (fast == slow) {
            slow = head;
            while (fast != slow) {
                fast = fast->next;
                slow = slow->next;
            }
            return slow;
        }
    }
    return NULL;
}


// 用于创建带有环的链表的辅助函数
ListNode* createCycleListNode(int* values, int size, int cycleIndex) {
    if (values == nullptr || size == 0) {
        return nullptr;
    }
    ListNode* head = new ListNode(values[0]);
    ListNode* tail = head, *cycleNode = nullptr;

    for (int i = 0; i < size; ++i) {
        ListNode* Node = new ListNode(values[i]);
        tail->next = Node;
        tail = Node;
        if (i == cycleIndex) {
            cycleNode = Node;
        }
    }
    if (cycleNode) {
        tail->next = cycleNode;
    }
    return head;

}

// 测试函数
void test(ListNode* head) {
    ListNode* cycleNode = detectCycle(head);
    if (cycleNode) {
        std::cout << "Cycle detected at node with value: " << cycleNode->val << std::endl;
    }
    else {
        std::cout << "No cycle detected." << std::endl;
    }
}

// 主函数
int main() {
    // 示例:创建一个链表,其中包含环
    int values[] = { 3, 2, 0, -4 };
    int cycleIndex = 2; // 环的索引,从0开始计数,这里假设环连接在第三个节点后
    int size = sizeof(values) / sizeof(values[0]);
    ListNode* head = createCycleListNode(values, size, cycleIndex);
    test(head);

    // 清理链表
    delete head->next->next->next;
    delete head->next->next;
    delete head->next;
    delete head;

    return 0;
}

在这里插入图片描述

方法二:哈希表

哈希表可以记录访问过的节点,如果遇到环,可以直接将入环的节点返回

#include <iostream>
#include <unordered_set>
using namespace std;

// 定义链表节点结构体
struct ListNode {
	int val;
	ListNode* next;
	ListNode(int x) : val(x), next(nullptr) {}
};

// 检测链表中是否有环,并返回环的入口节点

ListNode* detectCycle(ListNode* head) {
	unordered_set<ListNode*> seen;
	while (head != nullptr) {
		if (seen.count(head)) {
			return head;
		}
		seen.insert(head);
		head = head->next;
	}
	return nullptr;
}



// 用于创建带有环的链表的辅助函数
ListNode* createCycleListNode(int* values, int size, int cycleIndex) {
	if (values == nullptr || size == 0) {
		return nullptr;
	}
	ListNode* head = new ListNode(values[0]);
	ListNode* tail = head, * cycleNode = nullptr;

	for (int i = 0; i < size; ++i) {
		ListNode* Node = new ListNode(values[i]);
		tail->next = Node;
		tail = Node;
		if (i == cycleIndex) {
			cycleNode = Node;
		}
	}
	if (cycleNode) {
		tail->next = cycleNode;
	}
	return head;

}

// 测试函数
void test(ListNode* head) {
	ListNode* cycleNode = detectCycle(head);
	if (cycleNode) {
		std::cout << "Cycle detected at node with value: " << cycleNode->val << std::endl;
	}
	else {
		std::cout << "No cycle detected." << std::endl;
	}
}

// 主函数
int main() {
	// 示例:创建一个链表,其中包含环
	int values[] = { 3, 2, 0, -4 };
	int cycleIndex = 2; // 环的索引,从0开始计数,这里假设环连接在第三个节点后
	int size = sizeof(values) / sizeof(values[0]);
	ListNode* head = createCycleListNode(values, size, cycleIndex);
	test(head);

	// 清理链表
	delete head->next->next->next;
	delete head->next->next;
	delete head->next;
	delete head;

	return 0;
}

在这里插入图片描述

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值