Skip to content

Remove duplicated form sorted list ⅱ#3

Open
Rinta-Rinta wants to merge 6 commits intomainfrom
Remove-Duplicated-form-Sorted-List-Ⅱ

Hidden character warning

The head ref may contain hidden characters: "Remove-Duplicated-form-Sorted-List-\u2161"
Open

Remove duplicated form sorted list ⅱ#3
Rinta-Rinta wants to merge 6 commits intomainfrom
Remove-Duplicated-form-Sorted-List-Ⅱ

Conversation

@Rinta-Rinta
Copy link
Copy Markdown
Owner

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[fyi]

循環を見つける具体的な仕組み

Pythonでの具体的な実装は不明ですが、ひとつの実装は #1 でやりましたね。

Comment on lines 84 to 94
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

入口はChatGPTで良いのですが、自分は根拠になるドキュメントやコードのURLを出力させて、それを自分で読むようにしています。読まないまでもURLを記録しておくと、後から答え合わせや追加調査がやりやすいです。

Comment on lines 142 to 150
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的な好みですが、条件分岐ごとの処理が数行を超えるときは、片方をさっさと continue しちゃって、インデントを下げるほうが読みやすいです。

Suggested change
if ptr.next.val != ptr.next.next.val:
ptr = ptr.next
else:
duplicated_val = ptr.next.val
runner = ptr.next
while runner.next is not None and runner.next.val == duplicated_val:
runner.next = runner.next.next
ptr.next = runner.next
if ptr.next.val != ptr.next.next.val:
ptr = ptr.next
continue
duplicated_val = ptr.next.val
runner = ptr.next
while runner.next is not None and runner.next.val == duplicated_val:
runner.next = runner.next.next
ptr.next = runner.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.

コメントありがとうございます。確かに、処理が長いときはさっさとcontinueした方が読みやすいですね。勉強になります。

dummy = ListNode()
dummy.next = head

ptr = dummy
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ポインターの略だと思うのですが、 Python にはポインターという概念は存在しないため、 ptr と付けるのはやや違和感を感じました。 node と付ける方が多いように思います。


dummy = ListNode()
dummy.next = head
ptr = dummy
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これ、もう少しいい変数名ありませんかね。
このコードを初めて見た人が上から読んでいって、あ、ptr ってこういう役割だろうって分かりますか。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.fcs3httrll4l

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.

4 participants