Conversation
| public: | ||
| ListNode* deleteDuplicates(ListNode* head) { | ||
| ListNode* dummy_head = new ListNode(0, head); | ||
| ListNode* current = dummy_head; |
There was a problem hiding this comment.
自分は current という名前でもよいと思うのですが、他の方で current という単語にはあまり意味がないため、あえて current という変数名を付ける利点はないという方もいらっしゃいます。 node と付けても良いと思います。
There was a problem hiding this comment.
コメントありがとうございます。
なるほど、currentだけだと違和感があるというのは納得です。
Mike0121/LeetCode#7 (comment)
https://discord.com/channels/1084280443945353267/1225849404037009609/1234206158630289450
| current = current->next; | ||
| } | ||
| ListNode* result = dummy_head->next; | ||
| delete dummy_head; |
There was a problem hiding this comment.
new したオブジェクトをきちんと delete している点、とても良いと思います。
| class Solution { | ||
| public: | ||
| ListNode* deleteDuplicates(ListNode* head) { | ||
| ListNode dummy_head = ListNode(0, head); |
| while (current) { | ||
| ListNode* group_top = current->next; | ||
| while (group_top && group_top->next && group_top->val == group_top->next->val) { | ||
| int group_val =group_top->val; |
There was a problem hiding this comment.
コメントありがとうございます、typoでした 🙏
| } | ||
|
|
||
| private: | ||
|
|
There was a problem hiding this comment.
上記 public: 直下には空白がないので、合わせてこの空白も消した方が一貫性があって良いと思います。
There was a problem hiding this comment.
コメントありがとうございます、これは確かにそのとおりですね
一貫性の維持ができていませんでした。
| while (current) { | ||
| ListNode* group_top = current->next; | ||
| while (group_top && group_top->next && group_top->val == group_top->next->val) { | ||
| group_top = NextGroupTop(group_top); |
| while (current) { | ||
| ListNode* group_top = current->next; | ||
| while (group_top && group_top->next && group_top->val == group_top->next->val) { | ||
| int group_val =group_top->val; | ||
| while (group_top && group_top->val == group_val) { | ||
| group_top = group_top->next; | ||
| } | ||
| } | ||
| current->next = group_top; | ||
| current = current->next; | ||
| } |
There was a problem hiding this comment.
while 3重が気になりました
if 文を挟めば2重にできるかなと思います
| while (current) { | |
| ListNode* group_top = current->next; | |
| while (group_top && group_top->next && group_top->val == group_top->next->val) { | |
| int group_val =group_top->val; | |
| while (group_top && group_top->val == group_val) { | |
| group_top = group_top->next; | |
| } | |
| } | |
| current->next = group_top; | |
| current = current->next; | |
| } | |
| while (current) { | |
| ListNode* group_top = current->next; | |
| if (group_top && group_top->next && group_top->val == group_top->next->val) { | |
| int group_val = group_top->val; | |
| while (group_top && group_top->val == group_val) { | |
| group_top = group_top->next; | |
| } | |
| current->next = group_top; | |
| } else { | |
| current = current->next; | |
| } | |
| } |
There was a problem hiding this comment.
コメントありがとうございます。
3重whieは確かに微妙ですね・・・ifで別途書き直してみます。
| class Solution { | ||
| public: | ||
| ListNode* deleteDuplicates(ListNode* head) { | ||
| ListNode dummy_head = ListNode(-1, head); |
There was a problem hiding this comment.
sentinelという名前で表現することもできます
https://www.geeksforgeeks.org/doubly-linked-list-using-sentinel-nodes/
There was a problem hiding this comment.
なるほどありがとうございます。
sentinelはdummyよりも直感的ですね 🙏
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/