Skip to content

82. Remove Duplicates from Sorted List II#4

Open
Zun-U wants to merge 1 commit intomasterfrom
82/remove-duplicates-from-sorted-list-II
Open

82. Remove Duplicates from Sorted List II#4
Zun-U wants to merge 1 commit intomasterfrom
82/remove-duplicates-from-sorted-list-II

Conversation

@Zun-U
Copy link
Owner

@Zun-U Zun-U commented Mar 23, 2026


## STEP1
配列を別に作って、それを操作して重複が消えたものを改めてListNodeとして返却する方法を考えましたが、
まとまりがつかず5分が立ちましたので、新井氏の解説動画を見ました。
Copy link

@h-masder h-masder Mar 23, 2026

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/1371297545514647562/1374353329357848717

また、途中で解説動画を見たことで、もともとの考え方から解き方自体が変わっているように感じました。まずは、自分の思い描いた解法を(計算量を無視しても)最後まで書き切ってみることも大切だと思います。

最終的には、自力で考え、いくつかの選択肢を持ち、その中から「将来誰かがそのコードを見ても困らないもの」を選択できると良いと思います。

途中で詰まること自体は全く問題ありませんし、できあがったものだけを載せる必要もありません。レビュアーを活用しながら進めていただけたらと思います。

Copy link
Owner Author

@Zun-U Zun-U Mar 25, 2026

Choose a reason for hiding this comment

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

@h-masder
レビューいただきありがとうございます。
おっしゃる通り、もともと想定していた解き方は、動画と異なる方法を考えておりました。
今後はその過程も記載するようにいたします。


Listの最初にダミーのnodeを置く発想はありませんでした。
これを番兵(sentinel)と呼ばれることを初めて知りました。
番兵はheadより一歩遅れて進むことが分かると、理解が進みました。

Choose a reason for hiding this comment

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

番兵は分岐を減らすものですので、番兵はあってもなくても解くことはできます。
番兵を使わない方法も書いてみるとよいと思います。

node = node.Next
}
return sentinel.Next
}

Choose a reason for hiding this comment

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

コードとても読みやすくて良いと思います。
変数名ですが、preNodeよりもprevNodeのほうが適切かなと私は思いました。

*/
func deleteDuplicates(head *ListNode) *ListNode {
sentinel := &ListNode{Next: head}
preNode := sentinel
Copy link

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)

Copy link
Owner Author

Choose a reason for hiding this comment

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

@h-masder @nodchip
レビューありがとうございます。
preとpreviousの用語の意味があいまいになっておりました。
ご指摘のとおり、nodeの1つ前を表すという意味では、previous(prev)が適切と思いました。
変数の命名に対してもシビアに考えていければと思います。

Choose a reason for hiding this comment

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

previous や previous_node がよさそうですね。

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.

3 participants