-
Notifications
You must be signed in to change notification settings - Fork 0
141. Linked List Cycle #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ### step1 | ||
|
|
||
| グラフのサイクル検出アルゴリズムで解こうとしたが、実装が主いつなかったので、ChatGPTに提示されたフロイドの循環検出アルゴリズム(https://note.com/rhayahi/n/n7fc11c09fec6)で解いた。 | ||
|
|
||
| ### step2 | ||
|
|
||
| L11の部分をhead == nullptrに書き直した。 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. step2で参照したreferenceやGitHubなどリンクを貼っておくと後の学習に役立つと思います。 スタイルなどを参照したい時は下記も役に立つかと思います。あくまで参考ですが、一つ基準があると良いそうです。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメントありがとうございます!承知しました。 |
||
|
|
||
| ### step3 | ||
|
|
||
| step2のコードを3回正解できるまで書き直した。 | ||
| 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; | ||
| } | ||
| }; |
| 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; | ||
| } | ||
| }; |
| 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; | ||
| } | ||
| }; |
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この行はあってもいいですが、なくてもいいですね。(意図としては、よしあしがあるので書いても書かなくてもいいが、動作が変わるかを考えておきましょうということです。)
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
私のコメントはこれくらいです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.2k4z0wt6ytf9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
フロイドのうさぎとかめは知らなかったのですが、setの方での実装をsolution2.cppというファイルで書き直しておきました。