Skip to content

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

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

Conversation

bumbuboon
Copy link
Owner

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

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.

Copy link
Owner Author

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

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:
のほうが自然に感じます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにそうですね。自分もそのように感じます。

return head
```

こちらの方が短く書けてるが、実際に動かしてみると平均的に5msほど遅かった。
Copy link

Choose a reason for hiding this comment

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

LeetCode 上の実行時間は分散が大きいため、あまり気にしないほうがよいと思います。

@h1rosaka
Copy link

この方はいろんな再帰の書き方でこの問題を解いていたので、参考になるかもしれません。
https://github.com/goto-untrapped/Arai60/pull/42/files#diff-955136daee65dfdfb0d127b8407c9efa23cc6cb7260f7fdbfc6c4ea1d95b990eR59-R78

```python
if node2.val = visited:
node2 = node2.next
node.next = node2
Copy link

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を移動させるか、その逆かが異なる。
下の方が見やすい気もする。

次のような記法もあった。
Copy link

Choose a reason for hiding this comment

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

Discord の中に問題名をいれて検索するといろいろな過去の集積が出てくるのでそれをおすすめします。

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