Skip to content

solve: 82.Remove Duplicates from Sorted List II#10

Open
t9a-dev wants to merge 2 commits intomainfrom
82.Remove-Duplicates-from-Sorted-List-II
Open

solve: 82.Remove Duplicates from Sorted List II#10
t9a-dev wants to merge 2 commits intomainfrom
82.Remove-Duplicates-from-Sorted-List-II

Conversation

@t9a-dev
Copy link
Copy Markdown
Owner

@t9a-dev t9a-dev commented Oct 21, 2025

問題: 82. Remove Duplicates from Sorted List II
次に解く問題: 2. Add Two Numbers
ファイルの構成: ./src/bin/<各ステップ>.rs

Comment thread src/bin/step2.rs
他の想定ユースケース
-
- インプレイスでの実装を理解するのに以下の実装を参考にすることにしたが、結局理解ができなかった。
dummy,tailのうちtailがよくわからないのが原因だと考えられる。(コードが悪いということではない)
Copy link
Copy Markdown

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/1195700948786491403/1197102971977211966
コメント集漁るともう少しある気がしますがとりあえずこれを。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

番兵がむしろ分からないのかもしれません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。他の方のコードやレビューを確認しながら別の問題で実装に利用できたので理解できたと思います。
https://github.com/t9a-dev/LeetCode_arai60/pull/11/files#diff-02d815381f4af9ab10091ec503f6ff8fc63efd9ba6a1d06f2743d213c1a4879b

Comment thread src/bin/step1.rs
-
- Rustのイテレータメソッドのcontainsではカウントを返さないことを知った。
- 問題を見たときに思いついた解法では時間計算量がO(N^2)になっているが、O(N)にはできそうな感じはする。
- HashMapを利用してキーにノードの値、値に出現回数を入れていき、出現回数が1の値で返却用のリンクリストを生成する方法でできる。
Copy link
Copy Markdown

@TrsmYsk TrsmYsk Oct 21, 2025

Choose a reason for hiding this comment

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

この方法はリストがソート済みのときにしか使えないので、ソートされてないときも使える方法を考えてみてもいいかもしれません。
inplaceでつなぎかえる方法もソート済み前提ですね。忘れてください。


has_duplicate = true;
// 次のノードと重複しているので飛ばして、次の次のノードを次のノードにする。
node.next = next_node.next.take();
Copy link
Copy Markdown

@TrsmYsk TrsmYsk Oct 21, 2025

Choose a reason for hiding this comment

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

nextで形容される変数が2つ出てきているため、それぞれの変数についてどんな情報を伝えたいのかが良く分かりません。変数の名前を工夫できないか検討してみてください。

- 次ノードの次ノードをカーソルノードの次ノードの参照先に更新する。
- node[i].next = node[i+1].next
- ループの先頭に戻る
- 重複が発生していたかをフラグから判断
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

フラグは使わないほうが無難なので、処理を関数化できるかどうか検討してみてください。
Kaichi-Irie/leetcode-python#20 (comment)

Comment on lines +27 to +30
- 他の人の実装を見ているときにtailという変数名に違和感を覚えてしまい、解法のロジックが理解できない現象に遭遇した。
- 自分の中でtailは末尾を想起するものである。
- 入力として与えられたリンクリストのあるノードを指してtailといっているのは、末尾ではないだろうと考えてしまう。
- どうしても納得できなかったので、ChatGPTに相談して変数名をwrite_cursorにすることにした。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tailに違和感があるんですね。

in-place(というよりストリーミングといった方が正確でしょうか)に解く場合、入力のリストを線形に1回だけ舐めていき、解答に含めるかどうかをその場で判断していきます。走査の各時点において、出力することが確定したノード一覧(リスト)が1つに決まると思いますが、その末尾のことをtailと言っていますね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

pythonで恐縮ですが、以前似たようにtail関連のレビューをしたことがあり、よければご参考ください。

https://discordapp.com/channels/1084280443945353267/1318459221204930671/1423549410855354378

(擬似コード的なノリで眺めてもらえたらと思います。)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

in-place(というよりストリーミングといった方が正確でしょうか)に解く場合、入力のリストを線形に1回だけ舐めていき、解答に含めるかどうかをその場で判断していきます。

ありがとうございます。頂いた関連レビューと上記の説明で理解できました。(別の問題の実装で使えたので理解できているはず。)
https://github.com/t9a-dev/LeetCode_arai60/pull/11/files#diff-26ccac330cf02071c9820a981f57e47e8d7fbc46541becba86e1be21c665ff47
ストリーミング処理していることが自覚的でないことが原因だったと思いました。

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