Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

pineappleYogurt
Copy link
Owner

```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(1)

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)
Copy link

@olsen-blue olsen-blue Dec 30, 2024

Choose a reason for hiding this comment

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

(レビュー自体初なので何か変な操作をしていたらご一報いただけますと幸いです。)
「1」という値は先頭ノードの値なので、dummyに値を与えるのであれば、被らない値をセットする方が、明示的で良いのかもしれないと個人的には感じました。
私は「0」にしていました。「-1」とかでも良いのかもしれないです。

Copy link

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
Copy link

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にするかなと思います。

Comment on lines +7 to +15
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
Copy link

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
Copy link

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 が使われているために動かない)可能性があるでしょう。

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.

5 participants