Skip to content

82. Remove Duplicates from Sorted List II #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 2 commits into
base: main
Choose a base branch
from

Conversation

katataku
Copy link
Owner

pre.next = node
pre = node
node = node.next
return dummy.next
Copy link

Choose a reason for hiding this comment

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

pre.next について、ループを回している間、どのようなものであると思っているのかが、一定していないように見受けられます。

  • duplicate でないと確定したものだけ、pre.next につなぐのか
  • ひとまず、次に処理をするつもりであるものが pre.next にいるのか

dummy = ListNode(0, head) 後者
continue (の前の行にpre.next = nodeがない)前者
pre.next = node 後者
return dummy.next (の前の行にpre.next = nodeがない)前者

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。まさに、その二つは一定にできておらず、意識もできてませんでした。

nodchipさんのコメントの方に、内省など書きました。
https://github.com/katataku/leetcode/pull/3/files#r1836676340

Copy link

Choose a reason for hiding this comment

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

これはどちら側に合わせても書けるはずなので、書いてみて比較して比べてみるといいと思います。

よく、私は、ループを仕事の引き継ぎで例えているんです。
前の人は次の人に対して、何を約束して、仕事を引き継いでいるのかが、そのときの気分で変わっていると、引き継ぎを受けた人は困るということですね。

Copy link

Choose a reason for hiding this comment

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

Discord 内で「引き継ぎ」の話を結構しているので、よければ調べておいてください。

return self.deleteDuplicates(node)
```

↓が時間切れで解き切れなかったループの残骸。入力`[1,1]`でエラーになる。
Copy link

Choose a reason for hiding this comment

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

エラーになった原因は理解していますか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます。

エラーの原因がわかったかどうかですが、「わかった気がしていたが、odaさんのコメントを拝見して自信がなくなった」という感じです。。。

レビュー依頼時点と、odaさんのコメントを見た後での思考を言語化してみます。

  • レビュー依頼時点では、
    • 「あぁ漏れてるケースがあったね。よくよく考えて実装すれば、処理を追加してOKになった。次からは、Step3みたいな書き方をすればわかりやすく・間違いなくかけるね」ぐらいに捉えてました。
  • odaさんのコメントを見た時点では、
    • おっしゃる通り、preの意味を「注目してるものの前」ぐらいの意味で使っていおり、pre.nextの二つを完全に混同してました。
      • どちらかと言うと後者の意味で使ってましたが、頭の中では二つの違いを意識できてなかったです。
    • 今振り返ると、「Linked Listの要素ひとつづつ処理をしていく意識」みたいなものが薄かった気がしてます。
      • 今回の例で言うと、「preまでは確定しているんだ」とか「今からこれを処理するんだ」みたいな意識の話です。
      • だからこそ、僕はStep3のような「ひとつづつ処理してることがわかりやすい書き方」に寄っていったのかもと思いました。
        • 言い換えると、自分が「曖昧に扱っていた変数」があるコードを(無意識で)避けたのかなと思います。

改めて、現状の理解(エラーの原因がわかったかどうか)を言葉にすると、「この問題については、言語化して指摘をいただいたのでわかった。が、自分で言語化できなかった以上、本当の意味でわかってないのかもしれない」という状況です。。

Comment on lines +144 to +146
value_to_remove = node.val
while node is not None and node.val == value_to_remove:
node = node.next

Choose a reason for hiding this comment

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

この部分はヘルパー関数に切り出しても良いかもですね。

@konnysh
Copy link

konnysh commented Nov 13, 2024

step1については皆さんのコメントがあるのでstep2以降についてですが、読みやすかったので良いと思います!

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