Skip to content

82. Remove Duplicates from Sorted List II#20

Merged
colorbox merged 2 commits intomainfrom
82
Nov 12, 2024
Merged

82. Remove Duplicates from Sorted List II#20
colorbox merged 2 commits intomainfrom
82

Conversation

@colorbox
Copy link
Copy Markdown
Owner

@colorbox colorbox commented Jul 6, 2024

Copy link
Copy Markdown

@nodchip nodchip left a comment

Choose a reason for hiding this comment

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

よいと思います。

public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* dummy_head = new ListNode(0, head);
ListNode* current = dummy_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.

自分は current という名前でもよいと思うのですが、他の方で current という単語にはあまり意味がないため、あえて current という変数名を付ける利点はないという方もいらっしゃいます。 node と付けても良いと思います。

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.

コメントありがとうございます。
なるほど、currentだけだと違和感があるというのは納得です。

Mike0121/LeetCode#7 (comment)
https://discord.com/channels/1084280443945353267/1225849404037009609/1234206158630289450

current = current->next;
}
ListNode* result = dummy_head->next;
delete dummy_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.

new したオブジェクトをきちんと delete している点、とても良いと思います。

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode dummy_head = ListNode(0, 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.

こちらのように new を使わないほうがシンプルで良いと思います。

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;
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.

コメントありがとうございます、typoでした 🙏

}

private:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

上記 public: 直下には空白がないので、合わせてこの空白も消した方が一貫性があって良いと思います。

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.

コメントありがとうございます、これは確かにそのとおりですね
一貫性の維持ができていませんでした。

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);
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

Choose a reason for hiding this comment

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

真ん中の while が if でもいいような気がしますかね。

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.

確かに3重whileはくどい気がしますね・・・

Copy link
Copy Markdown

@Yoshiki-Iwasa Yoshiki-Iwasa left a comment

Choose a reason for hiding this comment

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

みました

Comment on lines +6 to +16
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;
}
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 3重が気になりました
if 文を挟めば2重にできるかなと思います

Suggested change
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;
}
}

Copy link
Copy Markdown
Owner Author

@colorbox colorbox Jul 12, 2024

Choose a reason for hiding this comment

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

コメントありがとうございます。
3重whieは確かに微妙ですね・・・ifで別途書き直してみます。

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode dummy_head = ListNode(-1, 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.

sentinelという名前で表現することもできます
https://www.geeksforgeeks.org/doubly-linked-list-using-sentinel-nodes/

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.

なるほどありがとうございます。
sentinelはdummyよりも直感的ですね 🙏

@colorbox colorbox merged commit 8385d3a into main Nov 12, 2024
@colorbox colorbox deleted the 82 branch November 12, 2024 00:56
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.

5 participants