✅✅Easy Solution ||Time Complexity 0(n)✅✅
If you like and understand my solution please upvote \uD83D\uDE0A\uD83D\uDE0A\n\t\n\t  \n\t\n# Intuition\nTo remove duplicates from a sorted linked list while m
## If you like and understand my solution please upvote \uD83D\uDE0A\uD83D\uDE0A\n\t\n\t  \n\t\n# Intuition\nTo remove duplicates from a sorted linked list while maintaining the order of distinct values, we can use a two-pointer approach. We iterate through the linked list, comparing each node\'s value with the value of its next node. If the values are equal, we skip over all nodes with the same value. Otherwise, we update pointers to maintain the list\'s integrity.\n\n# Approach\n1. Initialize a dummy node to handle edge cases and pointers `prev` and `temp` to track the last distinct node and the current node, respectively.\n2. Iterate through the linked list using `temp`.\n3. If `temp`\'s value is equal to its next node\'s value, skip over all nodes with the same value.\n4. Update pointers to maintain the list\'s integrity.\n5. Return the head of the modified list.\n\n# Complexity\n- Time complexity: \\(O(n)\\), where \\(n\\) is the number of nodes in the linked list. We traverse the list once to remove duplicates.\n- Space complexity: \\(O(1)\\). We use only a constant amount of extra space regardless of the size of the input linked list.\n- \n![upvote.jpeg](https://assets.leetcode.com/users/images/365515b7-fd26-4c96-897a-88eba6641bd4_1713260277.9304848.jpeg)\n\n# Code\n```\nclass Solution {\npublic:\n    ListNode* deleteDuplicates(ListNode* head) {\n          ListNode* dummy = new ListNode(0); // Dummy node to handle edge cases\n        dummy->next = head;\n        ListNode* prev = dummy; // Pointer to track the last distinct node\n        ListNode* temp = head;\n        \n        while (temp != nullptr && temp->next != nullptr) {\n            if (temp->val == temp->next->val) {\n                int data = temp->val;\n                while (temp != nullptr && temp->val == data) {\n                    ListNode* del = temp;\n                    temp = temp->next;\n                    delete del;\n                }\n                prev->next = temp; // Update the next pointer of the previous distinct node\n            } else {\n                prev = temp; // Move the previous pointer to the current node\n                temp = temp->next; // Move the current pointer to the next node\n            }\n        }\n        \n        head = dummy->next; // Update the head of the list\n        delete dummy; // Delete the dummy node\n        return head;\n    }\n};\n\n```
['Linked List', 'C++']