Skip to content

Commit 4a31a56

Browse files
committed
Add recursive code
1 parent f31a8ea commit 4a31a56

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

83/recursive.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
};

83/recursive2.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
};

0 commit comments

Comments
 (0)