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