Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 0141-linked-list-cycle/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### step1

グラフのサイクル検出アルゴリズムで解こうとしたが、実装が主いつなかったので、ChatGPTに提示されたフロイドの循環検出アルゴリズム(https://note.com/rhayahi/n/n7fc11c09fec6)で解いた。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

フロイドのうさぎとかめは知らなかったのですが、setの方での実装をsolution2.cppというファイルで書き直しておきました。


### step2

L11の部分をhead == nullptrに書き直した。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

step2で参照したreferenceやGitHubなどリンクを貼っておくと後の学習に役立つと思います。

スタイルなどを参照したい時は下記も役に立つかと思います。あくまで参考ですが、一つ基準があると良いそうです。
https://google.github.io/styleguide/cppguide.html

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます!承知しました。


### step3

step2のコードを3回正解できるまで書き直した。
26 changes: 26 additions & 0 deletions 0141-linked-list-cycle/solution2.cpp
Original file line number Diff line number Diff line change
@@ -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<ListNode*> visited;
ListNode* curr = head;

while (curr != nullptr) {
if (visited.count(curr)) {
return true;
}
visited.insert(curr);
curr = curr->next;
}

return false;
}
};
26 changes: 26 additions & 0 deletions 0141-linked-list-cycle/step1.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
26 changes: 26 additions & 0 deletions 0141-linked-list-cycle/step2.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
21 changes: 21 additions & 0 deletions 0141-linked-list-cycle/step3.cpp
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この行はあってもいいですが、なくてもいいですね。(意図としては、よしあしがあるので書いても書かなくてもいいが、動作が変わるかを考えておきましょうということです。)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

動作がどう変わるかは考えるようにします。

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