Skip to content

83. Remove Duplicates from Sorted List #2

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

katataku
Copy link
Owner

@katataku katataku commented Nov 8, 2024

Copy link

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

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

良いと思います。whileループの中で重複を見つけたら重複がなくなるまでSkipするみたいな実装(2重ループ)も選択肢としてはあるかもしれないですね。

if head is None:
return None
current = head
while current.next is not None:

Choose a reason for hiding this comment

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

正常系では、1回のループでノードが1つ進むとするとif文はcontinueして、elseを消すのもありかもしれないです。

while current.next is not None:
    if current.val == current.next.val:
         current.next = current.next.next
         continue
    current = current.next

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
「1回のループでノードが1つ進む感じが伝わる」のと「early returnみたいでスッキリする」ので、いいですね!

https://www.python.jp/news/wnpython311/inline-function.html


- Discordを見ていて`current`という変数の命名について無意識についけていたことに気づいた。別の候補として`current_node`や`current_focusing_node`を思いついた。しかし、今回は短い処理であり、今注目しているノードという点は伝わる範囲だと思うので、`current`という変数名を使うことにする。もっと長い処理場合は、より意味のこもった変数名にする。
Copy link

Choose a reason for hiding this comment

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

本当に上から読んでいって明らかならば node だけでもいいと思うんですよね。読み手に何を伝えたいか次第です。
for i in range(len(array)): と書かれていたら、添字の配列であろうとほぼ確信して次にいけるわけです。
これが長くても迷彩か擬態にしかなりません。
https://discord.com/channels/1084280443945353267/1200089668901937312/1209882689411223644
https://discord.com/channels/1084280443945353267/1225849404037009609/1234218057199784077

@konnysh
Copy link

konnysh commented Nov 8, 2024

良いと思います!
好みの話になるんですが、step3の初めにhead単体でNoneチェックするより、step2のwhile内でチェックが済む方が好きです。
ループごとのチェックになって、実行時間がどれくらい変わるのかは分かりませんが、多分誤差だと思います

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.

4 participants