Conversation
| ```python | ||
| class Solution: | ||
| def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| cur = head |
There was a problem hiding this comment.
変数名は原則フルスペルで書くことをお勧めいたします。
https://google.github.io/styleguide/pyguide.html#s3.16-naming
Function names, variable names, and filenames should be descriptive; avoid abbreviation.
| def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| cur = head | ||
| while cur: | ||
| while cur.next and cur.next.val == cur.val: |
There was a problem hiding this comment.
細かい点となり恐縮ですが、
while cur.next and cur.val == cur.next.val:
のほうが自然に感じます。
| return head | ||
| ``` | ||
|
|
||
| こちらの方が短く書けてるが、実際に動かしてみると平均的に5msほど遅かった。 |
There was a problem hiding this comment.
LeetCode 上の実行時間は分散が大きいため、あまり気にしないほうがよいと思います。
|
この方はいろんな再帰の書き方でこの問題を解いていたので、参考になるかもしれません。 |
| ```python | ||
| if node2.val = visited: | ||
| node2 = node2.next | ||
| node.next = node2 |
There was a problem hiding this comment.
step3のコードのように、変数を一つだけ(例えばnode)にしてやれば,
if node.val = visited:
node.next = node.next.nextだけで済みますし、こちらの方が直感的にわかりやすいと思います
| node.nextをつなげてからnode2を移動させるか、その逆かが異なる。 | ||
| 下の方が見やすい気もする。 | ||
|
|
||
| 次のような記法もあった。 |
There was a problem hiding this comment.
Discord の中に問題名をいれて検索するといろいろな過去の集積が出てくるのでそれをおすすめします。
https://leetcode.com/problems/remove-duplicates-from-sorted-list/submissions/1389503289/