Skip to content

83.-Remove-Duplicates-from-Sorted-List#3

Open
maeken4 wants to merge 3 commits into
mainfrom
83.-Remove-Duplicates-from-Sorted-List
Open

83.-Remove-Duplicates-from-Sorted-List#3
maeken4 wants to merge 3 commits into
mainfrom
83.-Remove-Duplicates-from-Sorted-List

Conversation

@maeken4
Copy link
Copy Markdown
Owner

@maeken4 maeken4 commented May 18, 2025

@maeken4 maeken4 changed the title 83 Remove Duplicates from Sorted List 83. Remove Duplicates from Sorted List May 18, 2025
@maeken4 maeken4 changed the title 83. Remove Duplicates from Sorted List 83.-Remove-Duplicates-from-Sorted-List May 18, 2025
while (currentNode != nullptr) {
// 次のノードの値が条件を満たさない場合、その次のノードにつなげる。
if (currentNode->next != nullptr &&
currentNode->val != currentNode->next->val) {
Copy link
Copy Markdown

@potrue potrue May 19, 2025

Choose a reason for hiding this comment

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

!=ではなくて==でしょうか。
あとこちらもインデントを下げるべきかもしれません。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

+1 on the equaity (supposed to be ==) :)

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.

@potrue @ryosuketc
コメントありがとうございます!正しく動かないコードで大変失礼しました…
ノードの値が連続するときの処理なのでご指摘通り==ですね。

ListNode* resultTail = resultHead;
while (currentNode != nullptr && currentNode->next != nullptr) {
if (currentNode->next != nullptr &&
currentNode->val == currentNode->next->val) {
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://google.github.io/styleguide/cppguide.html#Boolean_Expressions
ちょっとズバリという言及が見つけられなかったんですが、この辺を見る限りこの行のインデントは一個下げて条件が縦にそろって並ぶようにした方が良い気がします。

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.

vscodeのC++用のフォーマッタを入れたのですが行数制限で改行されてしまったようです。
settings.jsonで、
"C_Cpp.clang_format_style": "{ BasedOnStyle: Google, IndentWidth: 4 ,TabWidth: 4, UseTab: Always,AlignAfterOpenBracket: Align, ColumnLimit: 120}",
のようにしたらうまくいきました。(AlwaysAlignAfterOpenBracket)

while (currentNode != nullptr) {
// 次のノードの値が条件を満たさない場合、その次のノードにつなげる。
if (currentNode->next != nullptr &&
currentNode->val != currentNode->next->val) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

+1 on the equaity (supposed to be ==) :)

currentNode->val == currentNode->next->val) {
currentNode->next = currentNode->next->next;
} else {
ListNode* append = new ListNode(currentNode->next->val);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

他の変数との統一感を考えると、appendNodenewTail の方が自然かなと感じました。

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.

おっしゃる通りそちらのほうが統一された命名ですね。次の問題で採用してみます。

ListNode* resultHead = new ListNode(head->val);
ListNode* resultTail = resultHead;
while (currentNode != nullptr && currentNode->next != nullptr) {
if (currentNode->next != nullptr &&
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

while の条件と重複してませんかね?

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.

ご指摘ありがとうございます。20行目の判定は不要ですね。

}
ListNode* now = head;
ListNode* ans = new ListNode(head->val);
ListNode* ansNow = ans;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

C++ では変数名は lower_snake で書くのをよく見ます。チームの平均的な書き方に合わせることをお勧めいたします。

ListNode* now = head;
ListNode* ans = new ListNode(head->val);
ListNode* ansNow = ans;
while (now != nullptr && now->next != nullptr) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

while (now && now->next) { とも書けますが、明示的に nullptr と比較するよう書いたほうが分かりやすいかもしれません。

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.

Pythonで他の人が書いているコードを見るとそちらの書き方をよく見ますが、結局頭の中ではis not Noneに変換している気がするので明示的に書いています。


class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
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://google.github.io/styleguide/cppguide.html#Spaces_vs._Tabs

Use only spaces, and indent 2 spaces at a time.
We use spaces for indentation. Do not use tabs in your code. You should set your editor to emit spaces when you hit the tab key.

ただし、上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。

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.

インデントとタブとスペースの正確な違いを意識できていなかったです。
こちらタブ文字が入ってしまっているということをgithub上から確認できるのでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

空白部分をマウスでドラッグし、 1 文字ずつ範囲選択できるか、複数文字分まとめて選択されてしまうかで判断できるのですが…、毎回それを行うのは現実的ではなく…、難しいです…。

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.

ありがとうございます。試してみたら確かにできました!笑お手数おかけしました。

return nullptr;
}
ListNode* currentNode = head;
ListNode* resultHead = new ListNode(head->val);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

new したメモリを解放していないのが気になりました。業務のコードでメモリを解放しないコードを書くと、メモリリークとなり、プログラムの実行中にメモリ使用量が徐々に増えていきます。そして物理メモリを使い切りると、スワップアウトが発生し、処理速度が遅くなります。最終的にスワップメモリも使い切ると異常終了します。
new したメモリは明示的に解放することをお勧めします。また、業務のコードでは、 unique_ptr や shared_ptr といったスマートポインターの利用を検討することをお勧めいたします。

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.

@nodchip
元のデータを変えたくないときもあるかなと思い新しくノードを作る解法も作ってみました。しかし呼び出し元で開放しないといけなくなりますね…
スマートポインタを使う機会があまりなかったので積極的に使ってみます。

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