From 7921bf0f5d0107858abe65ae869126ab1fb7817a Mon Sep 17 00:00:00 2001 From: Keisuke KUDO <151166401+Apo-Matchbox@users.noreply.github.com> Date: Thu, 5 Mar 2026 13:45:33 -0500 Subject: [PATCH] 142. shadowing Add markdown file for Linked List Cycle II problem with C++ solution. --- 142/142. shadowing.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 142/142. shadowing.md diff --git a/142/142. shadowing.md b/142/142. shadowing.md new file mode 100644 index 0000000..76797d2 --- /dev/null +++ b/142/142. shadowing.md @@ -0,0 +1,32 @@ +# (Linked List Cycle II) [https://leetcode.com/problems/linked-list-cycle-ii/description/] +# language: C++ + +* 今回はset + +```cpp +#include +#include + +struct ListNode { + int val; + ListNode* next; + ListNode(int x) : val(x), next(nullptr) {} +}; + +class Solution { + public: + ListNode *detectCycle(ListNode *head) { + std::set reached; + ListNode* node = head; + while (node) { + if (reached.contains(node)) { + return node; + } + reached.insert(node); + node = node->next; + } + return nullptr; + } +}; +``` +