1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| class Solution { public: ListNode* deleteDuplicates(ListNode* head) { if (!head) return head; ListNode* slow = head, *fast = head; while(fast){ if(fast->val != slow->val){ slow->next = fast; slow = slow->next; } fast = fast->next; } slow->next = nullptr; return head; } };
class Solution { public: int removeDuplicates(vector<int>& nums) { if(nums.empty()) return 0; int slow = 0, fast = 0; while(fast < nums.size()){ if(nums[slow] != nums[fast]){ nums[++slow] = nums[fast]; } ++fast; } return slow + 1; } };
|