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; + } +}; +``` +