diff --git a/cpp/83-Remove-Duplicates-from-Sorted-List.cpp b/cpp/83-Remove-Duplicates-from-Sorted-List.cpp new file mode 100644 index 000000000..5d589ac49 --- /dev/null +++ b/cpp/83-Remove-Duplicates-from-Sorted-List.cpp @@ -0,0 +1,22 @@ +// Time Complexity is O(N). +// Space Complexity is O(1). + +class Solution { +public: + ListNode* deleteDuplicates(ListNode* head) { + ListNode * fast = head; + ListNode * slow = head; + + while(slow != NULL) + { + while(fast != NULL && slow->val == fast->val) + fast = fast -> next; + + slow->next = fast; + slow = slow -> next; + } + + return head; + + } +};