diff --git a/0141-linked-list-cycle/memo.md b/0141-linked-list-cycle/memo.md new file mode 100644 index 0000000..a87344e --- /dev/null +++ b/0141-linked-list-cycle/memo.md @@ -0,0 +1,11 @@ +### step1 + +グラフのサイクル検出アルゴリズムで解こうとしたが、実装が主いつなかったので、ChatGPTに提示されたフロイドの循環検出アルゴリズム(https://note.com/rhayahi/n/n7fc11c09fec6)で解いた。 + +### step2 + +L11の部分をhead == nullptrに書き直した。 + +### step3 + +step2のコードを3回正解できるまで書き直した。 \ No newline at end of file diff --git a/0141-linked-list-cycle/solution2.cpp b/0141-linked-list-cycle/solution2.cpp new file mode 100644 index 0000000..e6d41e8 --- /dev/null +++ b/0141-linked-list-cycle/solution2.cpp @@ -0,0 +1,26 @@ +// setを使った別解 + +// Definition for singly-linked list. +// struct ListNode { +// int val; +// ListNode *next; +// ListNode(int x) : val(x), next(NULL) {} +// }; + +class Solution { +public: + bool hasCycle(ListNode *head) { + set visited; + ListNode* curr = head; + + while (curr != nullptr) { + if (visited.count(curr)) { + return true; + } + visited.insert(curr); + curr = curr->next; + } + + return false; + } +}; \ No newline at end of file diff --git a/0141-linked-list-cycle/step1.cpp b/0141-linked-list-cycle/step1.cpp new file mode 100644 index 0000000..74d73fc --- /dev/null +++ b/0141-linked-list-cycle/step1.cpp @@ -0,0 +1,26 @@ +// Definition for singly-linked list. +// struct ListNode { +// int val; +// ListNode *next; +// ListNode(int x) : val(x), next(NULL) {} +// }; + +class Solution { +public: + bool hasCycle(ListNode *head) { + if (!head) return false; + + ListNode* slow = head; + ListNode* fast = head; + + while (fast != nullptr && fast->next != nullptr) { + slow = slow->next; + fast = fast->next->next; + + if (slow == fast) { + return true; + } + } + return false; + } +}; \ No newline at end of file diff --git a/0141-linked-list-cycle/step2.cpp b/0141-linked-list-cycle/step2.cpp new file mode 100644 index 0000000..e411c1c --- /dev/null +++ b/0141-linked-list-cycle/step2.cpp @@ -0,0 +1,26 @@ +// Definition for singly-linked list. +// struct ListNode { +// int val; +// ListNode *next; +// ListNode(int x) : val(x), next(NULL) {} +// }; + +class Solution { +public: + bool hasCycle(ListNode *head) { + if (head == nullptr) return false; + + ListNode* slow = head; + ListNode* fast = head; + + while (fast != nullptr && fast->next != nullptr) { + slow = slow->next; + fast = fast->next->next; + + if (slow == fast) { + return true; + } + } + return false; + } +}; \ No newline at end of file diff --git a/0141-linked-list-cycle/step3.cpp b/0141-linked-list-cycle/step3.cpp new file mode 100644 index 0000000..888c748 --- /dev/null +++ b/0141-linked-list-cycle/step3.cpp @@ -0,0 +1,21 @@ +// Definition for singly-linked list. +// struct ListNode { +// int val; +// ListNode *next; +// ListNode(int x) : val(x), next(NULL) {} +// }; + +class Solution { +public: + bool hasCycle(ListNode *head) { + if (head == nullptr) return false; + ListNode* fast = head; + ListNode* slow = head; + while (fast != nullptr && fast->next != nullptr) { + slow = slow->next; + fast = fast->next->next; + if (fast == slow) return true; + } + return false; + } +}; \ No newline at end of file