File tree Expand file tree Collapse file tree 5 files changed +132
-0
lines changed Expand file tree Collapse file tree 5 files changed +132
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Time: 21:00
3
+ Space: O(1)
4
+ Time: O(N)
5
+
6
+ 先頭要素も削除されうるため、返すためのダミーノードを用意する。
7
+ リスト全体をチェックしていくwhileループと、重複要素を削除するwhileループを入れ子にする。
8
+ 重複要素はすべて消さないといけないので、判定するためにvalを残す。
9
+ */
10
+ class Solution {
11
+ public:
12
+ ListNode* deleteDuplicates (ListNode* head) {
13
+ ListNode* dummy_head = new ListNode (0 , head);
14
+ ListNode* current = dummy_head;
15
+ while (current) {
16
+ while (current->next && current->next ->next && current->next ->val == current->next ->next ->val ) {
17
+ int target_val = current->next ->val ;
18
+ while (current->next ->val == target_val) {
19
+ current->next = current->next ->next ;
20
+ if (!current->next ) {
21
+ break ;
22
+ }
23
+ }
24
+ }
25
+ current = current->next ;
26
+ }
27
+ ListNode* result = dummy_head->next ;
28
+ delete dummy_head;
29
+ return result;
30
+ }
31
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ new演算子を使用せず、deleteを不要にした。
3
+ nextを何度も書くのに違和感があったのでgroup_top変数にした。
4
+ チェック済みの最後尾を指すcurrentと、チェック中/同一数値の先頭を指すのgroup_topと役割を分けてわかりやすくした
5
+
6
+ */
7
+ class Solution {
8
+ public:
9
+ ListNode* deleteDuplicates (ListNode* head) {
10
+ ListNode dummy_head = ListNode (0 , head);
11
+ ListNode* current = &dummy_head;
12
+
13
+ while (current) {
14
+ ListNode* group_top = current->next ;
15
+ while (group_top && group_top->next && group_top->val == group_top->next ->val ) {
16
+ int group_val = group_top->val ;
17
+ while (group_top && group_top->val == group_val) {
18
+ group_top = group_top->next ;
19
+ }
20
+ current->next = group_top;
21
+ }
22
+ current = current->next ;
23
+ }
24
+ return dummy_head.next ;
25
+ }
26
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ メソッド切り出しで多少読みやすくしたが、根本的なロジックは変わっていない。
3
+ */
4
+ class Solution {
5
+ public:
6
+ ListNode* deleteDuplicates (ListNode* head) {
7
+ ListNode dummy_head = ListNode (0 , head);
8
+ ListNode* current = &dummy_head;
9
+
10
+ while (current) {
11
+ ListNode* group_top = current->next ;
12
+ while (group_top && group_top->next && group_top->val == group_top->next ->val ) {
13
+ group_top = NextGroupTop (group_top);
14
+ }
15
+ current->next = group_top;
16
+ current = current->next ;
17
+ }
18
+ return dummy_head.next ;
19
+ }
20
+
21
+ private:
22
+
23
+ ListNode* NextGroupTop (ListNode* head) {
24
+ ListNode* current = head;
25
+ while (current && current->next && current->val == current->next ->val ) {
26
+ current = current->next ;
27
+ }
28
+ return current->next ;
29
+ }
30
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ ListNode* deleteDuplicates (ListNode* head) {
4
+ ListNode dummy_head = ListNode (-1 , head);
5
+ ListNode* current = &dummy_head;
6
+ while (current) {
7
+ ListNode* group_top = current->next ;
8
+ while (group_top && group_top->next && group_top->val == group_top->next ->val ) {
9
+ int group_val =group_top->val ;
10
+ while (group_top && group_top->val == group_val) {
11
+ group_top = group_top->next ;
12
+ }
13
+ }
14
+ current->next = group_top;
15
+ current = current->next ;
16
+ }
17
+ return dummy_head.next ;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ レビュー指摘を受けて修正したコード
3
+ 3重のwhileはそれで解けなくはないしパスするものの読みづらい
4
+ ifの代わりにwhileを使うと連続した同値グループをそのブロック内で処理できる利点があるが流石に3重whileは読みづらいのでダメ
5
+ ifを使用して、外側のループの中で処理する。
6
+ */
7
+ class Solution {
8
+ public:
9
+ ListNode* deleteDuplicates (ListNode* head) {
10
+ ListNode dummy_node = ListNode (-1 , head);
11
+ ListNode* inspecting_node = &dummy_node;
12
+ while (inspecting_node) {
13
+ ListNode* group_top = inspecting_node->next ;
14
+ if (group_top && group_top->next && group_top->val == group_top->next ->val ) {
15
+ int group_val = group_top->val ;
16
+ while (group_top && group_top->val == group_val) {
17
+ inspecting_node->next = group_top->next ;
18
+ group_top = group_top->next ;
19
+ }
20
+ } else {
21
+ inspecting_node = inspecting_node->next ;
22
+ }
23
+ }
24
+ return dummy_node.next ;
25
+ }
26
+ };
You can’t perform that action at this time.
0 commit comments