Skip to content

Remove duplicate from sorted list2 #5

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: main
Choose a base branch
from

Conversation

bumbuboon
Copy link
Owner

node.next = node.next.next
else:
node = node.next
return dummy.next

Choose a reason for hiding this comment

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

きれいで分かりやすいなと思いました。

return head

current = head
value_counter = defaultdict(int)

Choose a reason for hiding this comment

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

こういうやり方もできるんですね。参考になりました。

while current.next and value_counter[current.next.val] > 1:
current.next = current.next.next
if current.next:
current = current.next

Choose a reason for hiding this comment

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

while current だと分岐が減ってよりスッキリしそうです。

while current:
    while current.next and value_counter[current.next.val] > 1:
        current.next = current.next.next
    current = current.next

else:
node = node.next

return dummy.next

Choose a reason for hiding this comment

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

個人的にstep1の方が分かりやすいと思いました。
最後の重複が取り除けないからif is_duplicate: としていると思うので、意味のつながりでいくと while句 + if句 と else句で分かれると思います。でも、if-else句 の方が書き方としてより強いつながりがあるのかなと想定して読んだので、そこのずれが気になりました。

node = dummy
while node.next and node.next.next:
if node.next.val == node.next.next.val:
duplicateval = node.next.val
Copy link

Choose a reason for hiding this comment

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

変数名の英単語は _ 区切りにすることをお勧めいたします。

https://peps.python.org/pep-0008/#function-and-variable-names

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

https://google.github.io/styleguide/pyguide.html#316-naming

local_var_name

node = dummy
while node.next and node.next.next:
if node.next.val == node.next.next.val:
duplicateval = node.next.val
Copy link

Choose a reason for hiding this comment

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

while node.next and node.next.next and node.next.val == node.next.next.val:
    node.next.next = node.next.next.next
node.next = node.next.next

と、 2 個目以降の重複した要素を削除したあと、 1 個目の要素を削除するという方法もあります。

return dummy.next
```

## step2 読みやすくする&他の人の回答を見る
Copy link

Choose a reason for hiding this comment

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

もう少し色々な回答を探して読んでみたほうがいい気がします。

ここで学習したいことは「他人のコードから意図を読み取ること」で、思ったよりも「色々な書き方があって」、「それぞれが変形で移り変われる」なと感じて欲しいです。
たとえば、
https://discord.com/channels/1084280443945353267/1226508154833993788/1246022270984392724
大変だったら、多分誰かがまとめていると思います。

class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0)
dummy.next = head

Choose a reason for hiding this comment

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

ここはstep2以降のように、dummy = ListNode(0, head)で1行で書いてしまった方が個人的にはみやすいですね。

Choose a reason for hiding this comment

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

すみません、こちらコメントにありました。

return dummy.next
```

重複する要素を全て削除しなければいけない場合はハッシュマップだと二周しなければいけないから効率が悪い?

Choose a reason for hiding this comment

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

効率が何を指しているかによる気がするので明確にできると良いかと思います。(ループ数なのか、計算量なのか)

@h1rosaka
Copy link

他にも色々な解法があるので見てみると良いかもしれません。
https://github.com/h1rosaka/arai60/pull/6/files#diff-2872987854e9b11a32f662bae9bb67c6016d97009cc8cb81b69b15d8f988a7f9R119

```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(-1,head)

Choose a reason for hiding this comment

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

細かいですが、ListNode(-1, head)のように引数同士の間はスペースを開けた方が良さそうに思います。
pep8で引数同士の間のスペースの項目はパッと見つけられませんでしたが、多くの例でスペースが開けられています。
https://pep8-ja.readthedocs.io/ja/latest/

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.

6 participants