Skip to content

141. linked list cycle#4

Open
lightbanana wants to merge 4 commits into
mainfrom
141.-Linked-List-Cycle
Open

141. linked list cycle#4
lightbanana wants to merge 4 commits into
mainfrom
141.-Linked-List-Cycle

Conversation

@lightbanana

Copy link
Copy Markdown
Owner

Comment thread memo.md
def hasCycle(self, head: Optional[ListNode]) -> bool:
visited_node = set()
current_node = head
if head is None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分なら、コーナーケースで early return する場合、関数の一番上に書くと思います。理由は、関数宣言と early return の間に書かれた内容を、 early return のロジックを読む間、読み手が短期記憶に保持し続けなければならないためです。これは短期記憶の要領の無駄遣いになり、読み手にとって不要な認知負荷になると思います。

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.

ありがとうございます。納得しました。early returnの場合は関数の最初で書くように意識してみます。

Comment thread memo.md
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
visited_node = set()
current_node = head

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

current という単語に、あまり情報がないように思いました。単に node だけで十分だと思います。

current/previous/next と対比する場合には、付けても良いと思います。

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.

ありがとうございます!納得しました。形容詞を使う際は、対比するものがあるかどうかを意識して、対比先がなければ使わないようにしてみようと思います。

Comment thread memo.md
return True
else:
visited.add(head)
head = head.next

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご覧ください。
https://github.com/yukik8/leetcode/pull/6#discussion_r3287970237

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.

ありがとうございます。headが動くことに違和感があるという点に納得しました。連結リストを扱う際はheadは動かさず、別の変数に格納してから扱おうと思います。

Comment thread memo.md
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
visited = set()
while head:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
ksaito0629/leetcode_arai60#1 (comment)

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.

ありがとうございます。納得しました。boolの評価はis Noneなどの形で行うように意識します。

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.

2 participants