-
Notifications
You must be signed in to change notification settings - Fork 0
82. Remove Duplicates from Sorted List II #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| Time: 21:00 | ||
| Space: O(1) | ||
| Time: O(N) | ||
|
|
||
| 先頭要素も削除されうるため、返すためのダミーノードを用意する。 | ||
| リスト全体をチェックしていくwhileループと、重複要素を削除するwhileループを入れ子にする。 | ||
| 重複要素はすべて消さないといけないので、判定するためにvalを残す。 | ||
| */ | ||
| class Solution { | ||
| public: | ||
| ListNode* deleteDuplicates(ListNode* head) { | ||
| ListNode* dummy_head = new ListNode(0, head); | ||
| ListNode* current = dummy_head; | ||
| while (current) { | ||
| while (current->next && current->next->next && current->next->val == current->next->next->val) { | ||
| int target_val = current->next->val; | ||
| while (current->next->val == target_val) { | ||
| current->next = current->next->next; | ||
| if (!current->next) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| current = current->next; | ||
| } | ||
| ListNode* result = dummy_head->next; | ||
| delete dummy_head; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new したオブジェクトをきちんと delete している点、とても良いと思います。 |
||
| return result; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| new演算子を使用せず、deleteを不要にした。 | ||
| nextを何度も書くのに違和感があったのでgroup_top変数にした。 | ||
| チェック済みの最後尾を指すcurrentと、チェック中/同一数値の先頭を指すのgroup_topと役割を分けてわかりやすくした | ||
|
|
||
| */ | ||
| class Solution { | ||
| public: | ||
| ListNode* deleteDuplicates(ListNode* head) { | ||
| ListNode dummy_head = ListNode(0, head); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらのように new を使わないほうがシンプルで良いと思います。 |
||
| ListNode* current = &dummy_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; | ||
| while (group_top && group_top->val == group_val) { | ||
| group_top = group_top->next; | ||
| } | ||
| current->next = group_top; | ||
| } | ||
| current = current->next; | ||
| } | ||
| return dummy_head.next; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| メソッド切り出しで多少読みやすくしたが、根本的なロジックは変わっていない。 | ||
| */ | ||
| class Solution { | ||
| public: | ||
| ListNode* deleteDuplicates(ListNode* head) { | ||
| ListNode dummy_head = ListNode(0, head); | ||
| ListNode* current = &dummy_head; | ||
|
|
||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なんか、実質三重になっていませんか? (これでも動きそうですが。) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 真ん中の while が if でもいいような気がしますかね。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 確かに3重whileはくどい気がしますね・・・ |
||
| } | ||
| current->next = group_top; | ||
| current = current->next; | ||
| } | ||
| return dummy_head.next; | ||
| } | ||
|
|
||
| private: | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 上記
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメントありがとうございます、これは確かにそのとおりですね |
||
| ListNode* NextGroupTop(ListNode* head) { | ||
| ListNode* current = head; | ||
| while (current && current->next && current->val == current->next->val) { | ||
| current = current->next; | ||
| } | ||
| return current->next; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| class Solution { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| public: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ListNode* deleteDuplicates(ListNode* head) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ListNode dummy_head = ListNode(-1, head); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なるほどありがとうございます。 |
||||||||||||||||||||||||||||||||||||||||||||||||||
| ListNode* current = &dummy_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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. = の両脇にスペースを 1 つづつ空けましょう。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメントありがとうございます、typoでした 🙏 |
||||||||||||||||||||||||||||||||||||||||||||||||||
| while (group_top && group_top->val == group_val) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| group_top = group_top->next; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| current->next = group_top; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current->next; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+6
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while 3重が気になりました
Suggested change
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメントありがとうございます。 |
||||||||||||||||||||||||||||||||||||||||||||||||||
| return dummy_head.next; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| レビュー指摘を受けて修正したコード | ||
| 3重のwhileはそれで解けなくはないしパスするものの読みづらい | ||
| ifの代わりにwhileを使うと連続した同値グループをそのブロック内で処理できる利点があるが流石に3重whileは読みづらいのでダメ | ||
| ifを使用して、外側のループの中で処理する。 | ||
| */ | ||
| class Solution { | ||
| public: | ||
| ListNode* deleteDuplicates(ListNode* head) { | ||
| ListNode dummy_node = ListNode(-1, head); | ||
| ListNode* inspecting_node = &dummy_node; | ||
| while (inspecting_node) { | ||
| ListNode* group_top = inspecting_node->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) { | ||
| inspecting_node->next = group_top->next; | ||
| group_top = group_top->next; | ||
| } | ||
| } else { | ||
| inspecting_node = inspecting_node->next; | ||
| } | ||
| } | ||
| return dummy_node.next; | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分は
currentという名前でもよいと思うのですが、他の方で current という単語にはあまり意味がないため、あえて current という変数名を付ける利点はないという方もいらっしゃいます。 node と付けても良いと思います。There was a problem hiding this comment.
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