-
Notifications
You must be signed in to change notification settings - Fork 0
82. Remove Duplicates from Sorted List II #6
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?
Conversation
lc82.py
Outdated
dummy = ListNode(-1, head) | ||
node = dummy | ||
|
||
if node: |
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.
これwhile
じゃないですか??
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.
そうですね、間違えた箇所をコピペしてました、、
|
||
if node: | ||
if node.next and node.next.next and node.next.val == node.next.next.val: | ||
while node.next and node.next.next and node.next.val == node.next.next.val: |
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.
長い条件が二回連続して書かれているので、まとめるほうが好みです
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(-1, head)
node = dummy
while node:
is_duplicated = False
while node.next and node.next.next and node.next.val == node.next.next.val:
is_duplicated = True
node.next = node.next.next
if is_duplicated:
node.next = node.next.next
else:
node = node.next
return dummy.next
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.
そういう考えいいですね。条件文が長すぎるので何かないかなとずっと考えていたのでそのような案を提示してくださるのはあありがたいです。
操作としては特に何も変化はないですが条件を condition = node.next and node.next.next and node.next.val == node.next.next.val というふうにまとめるのも一つかなとは思ったんですけど、これについて可読性の観点から考えをお聞きしてもよろしいでしょうか?
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.
うーん その場合も可読性にはあまり寄与しないと思います
読み手としては、node.next = node.next.next
をする時に、重複ノードが消える前のこと(ifの条件)を頭に入れておかないといけないので、認知不可が高いんじゃないでしょうか?
例えば、手作業でこの重複削除の作業をすることを考えると、重複を1つにまとめる作業と最後に残った一つを消す作業の二段階に分かれるのではないかと思っていて
その場合って、重複ノードをバーっと消していって、その後で「じゃあ最後に残った一つも消すか」ってやると思うんですよ
「最初の段階でこのノードは重複してて、今最後の一個まで消したから、最後の一個も消そう」とは考えないはずで
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.
あ、これ発想になかったですね。
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.
この方法だと綺麗にまとめられますね!私が解いた時もできていなかったので勉強になりました。
みました! 再帰的に解く方法もあるので、ちょろっと見てみるといいかもです |
確認しました。私のプルリクを見てくださっていたようなので、ご存知かとは思いますが、tailを使った書き方もできます。たくさんnextを書かずにスッキリ書くことができて、個人的にはこちらが好みです。 |
while node: | ||
if node.next and node.next.next and node.next.val == node.next.next.val: | ||
while node.next and node.next.next and node.next.val == node.next.next.val: | ||
node.next = node.next.next | ||
node.next = node.next.next | ||
else: | ||
node = node.next |
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.
early return (この場合は early continue と言うのでしょうか) にすると条件判定部が少し読みやすくなると思いました。
while node: | |
if node.next and node.next.next and node.next.val == node.next.next.val: | |
while node.next and node.next.next and node.next.val == node.next.next.val: | |
node.next = node.next.next | |
node.next = node.next.next | |
else: | |
node = node.next | |
while node.next and node.next.next: | |
if node.next.val != node.next.next.val: | |
node = node.next | |
continue | |
while node.next and node.next.next and node.next.val == node.next.next.val: | |
node.next = node.next.next | |
node.next = node.next.next |
問題: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/
言語: Python
レビューをお願いします。