83.-Remove-Duplicates-from-Sorted-List#3
Conversation
| while (currentNode != nullptr) { | ||
| // 次のノードの値が条件を満たさない場合、その次のノードにつなげる。 | ||
| if (currentNode->next != nullptr && | ||
| currentNode->val != currentNode->next->val) { |
There was a problem hiding this comment.
!=ではなくて==でしょうか。
あとこちらもインデントを下げるべきかもしれません。
There was a problem hiding this comment.
+1 on the equaity (supposed to be ==) :)
There was a problem hiding this comment.
@potrue @ryosuketc
コメントありがとうございます!正しく動かないコードで大変失礼しました…
ノードの値が連続するときの処理なのでご指摘通り==ですね。
| ListNode* resultTail = resultHead; | ||
| while (currentNode != nullptr && currentNode->next != nullptr) { | ||
| if (currentNode->next != nullptr && | ||
| currentNode->val == currentNode->next->val) { |
There was a problem hiding this comment.
https://google.github.io/styleguide/cppguide.html#Boolean_Expressions
ちょっとズバリという言及が見つけられなかったんですが、この辺を見る限りこの行のインデントは一個下げて条件が縦にそろって並ぶようにした方が良い気がします。
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
+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); |
There was a problem hiding this comment.
他の変数との統一感を考えると、appendNode や newTail の方が自然かなと感じました。
There was a problem hiding this comment.
おっしゃる通りそちらのほうが統一された命名ですね。次の問題で採用してみます。
| ListNode* resultHead = new ListNode(head->val); | ||
| ListNode* resultTail = resultHead; | ||
| while (currentNode != nullptr && currentNode->next != nullptr) { | ||
| if (currentNode->next != nullptr && |
There was a problem hiding this comment.
ご指摘ありがとうございます。20行目の判定は不要ですね。
| } | ||
| ListNode* now = head; | ||
| ListNode* ans = new ListNode(head->val); | ||
| ListNode* ansNow = ans; |
There was a problem hiding this comment.
C++ では変数名は lower_snake で書くのをよく見ます。チームの平均的な書き方に合わせることをお勧めいたします。
| ListNode* now = head; | ||
| ListNode* ans = new ListNode(head->val); | ||
| ListNode* ansNow = ans; | ||
| while (now != nullptr && now->next != nullptr) { |
There was a problem hiding this comment.
while (now && now->next) { とも書けますが、明示的に nullptr と比較するよう書いたほうが分かりやすいかもしれません。
There was a problem hiding this comment.
Pythonで他の人が書いているコードを見るとそちらの書き方をよく見ますが、結局頭の中ではis not Noneに変換している気がするので明示的に書いています。
|
|
||
| class Solution { | ||
| public: | ||
| ListNode* deleteDuplicates(ListNode* head) { |
There was a problem hiding this comment.
インデントをタブで表現するのは、最近はあまり見ないかもしれません。
参考までにスタイルガイドへのリンクを貼ります。
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.
ただし、上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。
There was a problem hiding this comment.
インデントとタブとスペースの正確な違いを意識できていなかったです。
こちらタブ文字が入ってしまっているということをgithub上から確認できるのでしょうか?
There was a problem hiding this comment.
空白部分をマウスでドラッグし、 1 文字ずつ範囲選択できるか、複数文字分まとめて選択されてしまうかで判断できるのですが…、毎回それを行うのは現実的ではなく…、難しいです…。
There was a problem hiding this comment.
ありがとうございます。試してみたら確かにできました!笑お手数おかけしました。
| return nullptr; | ||
| } | ||
| ListNode* currentNode = head; | ||
| ListNode* resultHead = new ListNode(head->val); |
There was a problem hiding this comment.
new したメモリを解放していないのが気になりました。業務のコードでメモリを解放しないコードを書くと、メモリリークとなり、プログラムの実行中にメモリ使用量が徐々に増えていきます。そして物理メモリを使い切りると、スワップアウトが発生し、処理速度が遅くなります。最終的にスワップメモリも使い切ると異常終了します。
new したメモリは明示的に解放することをお勧めします。また、業務のコードでは、 unique_ptr や shared_ptr といったスマートポインターの利用を検討することをお勧めいたします。
There was a problem hiding this comment.
@nodchip
元のデータを変えたくないときもあるかなと思い新しくノードを作る解法も作ってみました。しかし呼び出し元で開放しないといけなくなりますね…
スマートポインタを使う機会があまりなかったので積極的に使ってみます。
This Problem
https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/