Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

141. Linked List Cycle #2

wants to merge 5 commits into from

Conversation

pineappleYogurt
Copy link
Owner

@pineappleYogurt pineappleYogurt commented Jul 22, 2024

解いた問題

Linked List Cycle - LeetCode - https://leetcode.com/problems/linked-list-cycle/description/

次に解く問題

Linked List Cycle II - LeetCode - https://leetcode.com/problems/linked-list-cycle-ii/description/

if head == None :
return False
fast = slow = head
while fast != None and fast.next != None:
Copy link

Choose a reason for hiding this comment

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

None との比較は is, is not が好まれますね。
理由は調べましょう。

そのまま条件にするのも一つです。
fast and fast.next のように。

Copy link
Owner Author

@pineappleYogurt pineappleYogurt Jul 22, 2024

Choose a reason for hiding this comment

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

None との比較は is, is not が好まれますね。
理由は調べましょう。

なるほど、Pythonでは同値性が==で同一性がisなんですね!
知らなかったです、ありがとうございます!

そのまま条件にするのも一つです。
fast and fast.next のように。

確かに今回はそれで条件として問題なさそう&人間の認知的に否定は避けたほうがいいというのを聞いたことがあるのでそちらでも良さそうですね👀
自分としては、今回提示されている条件に

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

とあったので、何も無い時はNoneと明示的に指定したほうがいいのかなとも思いましたがいかがでしょう?

Copy link

Choose a reason for hiding this comment

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

PEP と Google Style Guide を引用しておきます。

https://peps.python.org/pep-0008/#programming-recommendations

Comparisons to singletons like None should always be done with is or is not, never the equality operators.
Also, beware of writing if x when you really mean if x is not None

https://google.github.io/styleguide/pyguide.html#2144-decision

Always use if foo is None: (or is not None) to check for a None value.

ここで大事なのは、相手を説得するときに何をもってよしあしを論じるかです。結論はどちらでも構いません。

Copy link

Choose a reason for hiding this comment

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

同一性(identity)と同値性(equality)、知らなかったので勉強になりました。

```python
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
if head == None :
Copy link

Choose a reason for hiding this comment

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

この条件ないとどうなりますか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

アーリーリターンがなくなるため、可読性及び処理の効率が低下する認識です!

Copy link

Choose a reason for hiding this comment

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

ああ、なるほど。行数が短くなるので削ってもいいと思います。趣味の範囲ですね。

```
#### 思考ログ
- step2と同じ
- 回答わかっているのになぜ3回連続なんだろうと思ったが、確かに詰まっているところはしっかり理解しきれていないところだと思った。
Copy link

Choose a reason for hiding this comment

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

あと、調べる練習をするといいと思います。Discord 漁るなどです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

こちら、過去に問題を解かれた方々の回答を漁るということでしょうか?

Copy link

Choose a reason for hiding this comment

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

そうですね。そうです。

これは、「同僚の8,9割ができることを8,9割できるか、8,9割がする反応を8,9割するか」を練習しています。
https://docs.google.com/presentation/d/1Ny4kmHE2FZMI0AuPxImokweGoAE73RAGivjDJg0kG80/edit#slide=id.g2194639112d_0_647
https://hayapenguin.com/notes/Posts/2024/04/24/how-to-practice-coding-effectively

@Yoshiki-Iwasa
Copy link

本問題とは関係ないですが、PRがDraftのままになってます
レビューを出すときはReady for Reviewにして出すのがGithubの通常の使い方だと思います(^ ^)

(レビューワーもPRがDraftだと"おや?概念整理の部分だけみればいいのかな?"となる。今回はleetcodeのレビューだとわかってるのでこれは発生しませんが)

@pineappleYogurt pineappleYogurt marked this pull request as ready for review July 23, 2024 00:23
@hroc135
Copy link

hroc135 commented Jul 23, 2024

Tortoise and Hareで解く方が空間計算量がO(1)と効率が良いのですが、ハッシュマップを使う方が発想としては自然だと思います。
実際、私自身同様の指摘をいただきました。
hroc135/leetcode#1 (review)

@@ -0,0 +1,57 @@
# step1
Copy link

Choose a reason for hiding this comment

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

この解法をは初めから知っていましたか?何かを参照しましたか?自分で思いつきましたか?

初めから知っていたのであれば、どこかのタイミングでたまたま見聞きしたか、教科書等で学んだという事なのかなと思いました。
何かを参照して初めて知ったのであれば、普通だと思います。
自分で思いついたのだとしたら、何か途轍もないことが起こっている気がします。

Copy link
Owner Author

Choose a reason for hiding this comment

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

5分考えて全然わからなかったのであらいさんの回答を見ました🙏

Copy link

Choose a reason for hiding this comment

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

ありがとうございます。他の回答を見たときは、そのことを思考ログに残していただけるとありがたいです。理由は、コードを書いた方が、どのような経緯をたどってこのコードを書いたかが分かったほうが、アドバイスがしやすいためです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、承知しました!
次回から記載するようにします🙏

return False
fast = slow = head
while fast is not None and fast.next is not None:
Copy link

@Mike0121 Mike0121 Jul 24, 2024

Choose a reason for hiding this comment

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

可読性を考えると、while fast and fast.nextで良いと思いました。

if head is None :
return False
fast = slow = head

Choose a reason for hiding this comment

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

好みの問題ですが、自分だったらslow = fast = head (小さい、遅い方を先に持ってくる)の書き方にするかと思います。

def hasCycle(self, head: Optional[ListNode]) -> bool:
visited_nodes = set()
current_node = head

Choose a reason for hiding this comment

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

好みですが、今回コードが短くスコープが狭いので、visited, currentでも良いかなと思いました。
変数の長さに関して下記に参考になるかと思います。
https://discord.com/channels/1084280443945353267/1200089668901937312/1209882689411223644

@pineappleYogurt pineappleYogurt changed the title 141 141. Linked List Cycle Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants