回答編集履歴
2
追記
answer
CHANGED
@@ -15,12 +15,41 @@
|
|
15
15
|
current = head;
|
16
16
|
while(current != tail){
|
17
17
|
if(current->birthday % 2 == 0){
|
18
|
-
Node* del = current;
|
19
18
|
*now = current->next;
|
20
|
-
delete
|
19
|
+
delete current;
|
21
20
|
} else {
|
22
21
|
now = ¤t->next;
|
23
22
|
}
|
24
23
|
current = *now;
|
25
24
|
}
|
26
|
-
```
|
25
|
+
```
|
26
|
+
|
27
|
+
|
28
|
+
---
|
29
|
+
|
30
|
+
|
31
|
+
なんとなく、一応解説
|
32
|
+
|
33
|
+
```c
|
34
|
+
Node keeper;
|
35
|
+
keeper.next = head;
|
36
|
+
Node* last = &keeper;
|
37
|
+
// last は最後に調べた要素
|
38
|
+
while(last->next != tail){
|
39
|
+
current = last->next;
|
40
|
+
if(current->birthday % 2 == 0){
|
41
|
+
// currentの誕生日は偶数なので飛ばす
|
42
|
+
last->next = current->next;
|
43
|
+
delete current;
|
44
|
+
// last->nextを調べてないのでlastを更新しない
|
45
|
+
}
|
46
|
+
else
|
47
|
+
// last->nextを調べたのでlastを更新する
|
48
|
+
last = current;
|
49
|
+
}
|
50
|
+
head = keeper.next;
|
51
|
+
```
|
52
|
+
|
53
|
+
片方向リストは先頭要素を指すダミーを作ってしまうと↑のようになります。
|
54
|
+
|
55
|
+
よくみると、`last`は`last->next`にしか使ってないのでポインタでどうにかなるってのが提示したソースです。
|
1
コード修正
answer
CHANGED
@@ -14,13 +14,13 @@
|
|
14
14
|
Node** now = &head;
|
15
15
|
current = head;
|
16
16
|
while(current != tail){
|
17
|
-
if(current->birthday % 2 == 0){
|
17
|
+
if(current->birthday % 2 == 0){
|
18
|
-
|
18
|
+
Node* del = current;
|
19
|
-
|
19
|
+
*now = current->next;
|
20
|
-
|
20
|
+
delete del;
|
21
|
-
} else {
|
21
|
+
} else {
|
22
|
-
|
22
|
+
now = ¤t->next;
|
23
|
-
}
|
23
|
+
}
|
24
24
|
current = *now;
|
25
25
|
}
|
26
26
|
```
|