problem
solution
option 1 - Two Pointers
1 | class Solution { |
- other version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head) return head;
ListNode *ret = new ListNode(-1), *ans = ret;
ret->next = new ListNode(head->val);
ret = ret->next;
head= head->next;
while(head){
if(head->val!=ret->val){
ret->next = head;
ret =ret->next;
}
head= head->next;
}
ret->next = nullptr;
return ans->next;
}
};
option 2 - improve option 1
1 | class Solution { |
option 3 - use STL
1 | class Solution { |
analysis
- time complexity
O(n)
- space complexity
O(1)