Skip to content

Commit b0f17ca

Browse files
committed
1st
1 parent 61a30f4 commit b0f17ca

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

83/step1.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
4:30
3+
Time: O(N)
4+
Space: O(1)
5+
6+
リストの性質上、削除すべき要素は隣接している。
7+
リストを頭から終わりまでチェックし、不要要素を削除していくことで実現できると判断。
8+
9+
*/
10+
class Solution {
11+
public:
12+
ListNode* deleteDuplicates(ListNode* head) {
13+
ListNode* current = head;
14+
while (current) {
15+
if (!current || !current->next) {
16+
return head;
17+
}
18+
if (current->val == current->next->val) {
19+
current->next = current->next->next;
20+
} else {
21+
current = current->next;
22+
}
23+
}
24+
return head;
25+
}
26+
};

0 commit comments

Comments
 (0)