File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ 再帰によるコードを追加、同一値のグループごとに再帰させる。
3
+ 返り値を活用できていないのが気になる。
4
+ */
5
+ class Solution {
6
+ public:
7
+ ListNode* deleteDuplicates (ListNode* head) {
8
+ if (!head) return nullptr ;
9
+ while (head && head->next && head->val == head->next ->val ) {
10
+ head->next = head->next ->next ;
11
+ }
12
+ deleteDuplicates (head->next );
13
+ return head;
14
+ }
15
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ 再帰の返り値が使用されるように修正
3
+ */
4
+ class Solution {
5
+ public:
6
+ ListNode* deleteDuplicates (ListNode* head) {
7
+ if (!head) return nullptr ;
8
+ ListNode* current = head;
9
+ while (current && current->next && current->val == current->next ->val ) {
10
+ current = current->next ;
11
+ }
12
+ current = current->next ;
13
+ head->next = deleteDuplicates (current);
14
+ return head;
15
+ }
16
+ };
You can’t perform that action at this time.
0 commit comments