Skip to content

Commit 6ea80a9

Browse files
committed
Add double nest while code
1 parent 1d5a358 commit 6ea80a9

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

82/step4_with_if.cpp

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

0 commit comments

Comments
 (0)