Skip to content

83. Remove Duplicates from Sorted List #4

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 2 commits into
base: main
Choose a base branch
from
Open

83. Remove Duplicates from Sorted List #4

wants to merge 2 commits into from

Conversation

pineappleYogurt
Copy link
Owner

@pineappleYogurt pineappleYogurt changed the title add filw 83. Remove Duplicates from Sorted List Dec 10, 2024
Comment on lines +52 to +56
- 再帰で解く方法もあるそう
- 普段あまり再帰でコードを書く機会がないが再帰でしか書けないコード以外で再帰を使う意味はあるのか
- 個人的には再帰に慣れていないからか読みにくい
- nodchipさんのコメントでも以下のようにあった
>あくまで個人的な意見なのですが、ループと再帰で同じ処理が実装できる場合は、ループのほうが読んでいて認知負荷が低いように思います。個人的には step2 の回答のほうが好みです。

Choose a reason for hiding this comment

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

感想みたいなコメント失礼します。

再帰に慣れていないからか読みにくい

僕自身が「ループと関数なら、関数の方がわかりやすい」と感じていたので、「再帰に慣れていないからか読みにくい」というコメントが学びになります。ありがとうございます。

関数のデメリットとして「読み出し回数上限」や「関数呼び出しのオーバーヘッドがある」ため、どちらでもいいなら、ループを採用していいように思います。(チームメンバーに合わせる前提で。)
katataku/leetcode#7 (comment)

```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
copy = head
Copy link

Choose a reason for hiding this comment

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

copy は標準ライブラリーにある名前なので、できれば被せたくないです。
https://docs.python.org/3/library/copy.html
あとから使いたくなるかもしれないし、こちらを指していると勘違いするかもしれないからです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかに、標準ライブラリと被る点は考慮漏れでした。
currentが適切だったかなと考えています

```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
copy = head
Copy link

Choose a reason for hiding this comment

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

手順はわかりやすくていいと思います。

copyという変数名ですが、コピー元であるはずのheadからなるlinked-listを書き換えられてしまう違和感を感じました。

Copy link
Owner Author

@pineappleYogurt pineappleYogurt Dec 12, 2024

Choose a reason for hiding this comment

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

今回の使い方だとcurrentとかが適切だったかもしれないですね。
それか下で書いた通りdeep copyを行うかですかね。
https://github.com/pineappleYogurt/leetCode/pull/4/files#diff-cf676d39885ed53f4bdd78ac45cac8597da83e44027d741c99ec921bb306b61eR45

```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
Copy link

Choose a reason for hiding this comment

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

あくまで参考にはなりますが、Noneとの比較はis Noneもしくはis not Noneを使えというスタイルも存在します。

Copy link
Owner Author

Choose a reason for hiding this comment

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

あ、これは前回指摘されたところ・・・
教えていただきありがとうございます!

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