-
Notifications
You must be signed in to change notification settings - Fork 0
82. Remove Duplicates from Sorted List II #5
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
```python | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
dummy = ListNode(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.
最初の値は使わないのでdummy = ListNode(next=head)と書くこともできます。
```python | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
dummy = ListNode(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.
(レビュー自体初なので何か変な操作をしていたらご一報いただけますと幸いです。)
「1」という値は先頭ノードの値なので、dummyに値を与えるのであれば、被らない値をセットする方が、明示的で良いのかもしれないと個人的には感じました。
私は「0」にしていました。「-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.
そもそも指定しないというのもありかなと思います。
この場合は、dummy = ListNode(next=head)
とすると、一行分短縮もできます。
https://docs.python.org/3/glossary.html#term-argument
if node.next.val != node.next.next.val: | ||
node = node.next | ||
continue | ||
duplicate = 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.
duplicateしているのはnodeではなく値なので、自分なら、duplicate_value = node.next.valにするかなと思います。
current = dummyHead | ||
while current.next and current.next.next: | ||
if current.next.val == current.next.next.val: | ||
copy = current.next | ||
while copy.next and copy.val == copy.next.val: | ||
copy = copy.next | ||
current.next = copy.next | ||
else: | ||
current = current.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.
私はこのループはちょっと抵抗感があります。
というのは、これ current の意味付けが、取っておくことが決まったノードの最後ですね。
なので current.next という形ですべて出てきていますね。
これ、自然言語で説明したときに、取っておくもののお尻を「今」と呼んで、今の次のやつの性質を論じないと思うのです。
素直にやると、取っておくもののお尻、と、注目していて重複しているかを確認しているノードの2つに名前をつけて、重複していないことが分かったら、取っておくもののお尻に付け足すというほうが自然でしょう。
また、取っておくもののお尻.next = None にするほうが私は表現としては素直だと思います。
``` | ||
#### 思考ログ | ||
- step1では新井さんの解説動画の変数名をそのまま使ったが、copyなどの変数名は前回のレビューで指摘された通り汎用的すぎるので別の名前を考える。 | ||
- copy → duplicate |
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.
copy は、標準ライブラリーの名前と衝突しているのが少し気にかかります。
import copy したときに、copy という名前がぶつかります。
あとで、コードを編集をして copy を使いたくなった人が混乱する(import copy して使おうとしたのにそのスコープで copy が使われているために動かない)可能性があるでしょう。
解いた問題
82. Remove Duplicates from Sorted List II
次に解く問題
2. Add Two Numbers