-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
- 再帰で解く方法もあるそう | ||
- 普段あまり再帰でコードを書く機会がないが再帰でしか書けないコード以外で再帰を使う意味はあるのか | ||
- 個人的には再帰に慣れていないからか読みにくい | ||
- nodchipさんのコメントでも以下のようにあった | ||
>あくまで個人的な意見なのですが、ループと再帰で同じ処理が実装できる場合は、ループのほうが読んでいて認知負荷が低いように思います。個人的には step2 の回答のほうが好みです。 |
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.
感想みたいなコメント失礼します。
再帰に慣れていないからか読みにくい
僕自身が「ループと関数なら、関数の方がわかりやすい」と感じていたので、「再帰に慣れていないからか読みにくい」というコメントが学びになります。ありがとうございます。
関数のデメリットとして「読み出し回数上限」や「関数呼び出しのオーバーヘッドがある」ため、どちらでもいいなら、ループを採用していいように思います。(チームメンバーに合わせる前提で。)
katataku/leetcode#7 (comment)
```python | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
copy = head |
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 は標準ライブラリーにある名前なので、できれば被せたくないです。
https://docs.python.org/3/library/copy.html
あとから使いたくなるかもしれないし、こちらを指していると勘違いするかもしれないからです。
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が適切だったかなと考えています
```python | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
copy = head |
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
という変数名ですが、コピー元であるはずのheadからなるlinked-listを書き換えられてしまう違和感を感じました。
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とかが適切だったかもしれないですね。
それか下で書いた通り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: |
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.
あくまで参考にはなりますが、Noneとの比較はis None
もしくはis not None
を使えというスタイルも存在します。
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.
あ、これは前回指摘されたところ・・・
教えていただきありがとうございます!
解いた問題
83. Remove Duplicates from Sorted List
次に解く問題
82. Remove Duplicates from Sorted List II