Easy and Simple C++ Solution | Beats 100% ✅ | Detailed Explanation 🔥🔥🔥
Problem Statement:\nGiven the head of a sorted linked list, remove all nodes that have duplicate numbers, leaving only distinct numbers from the original list. 
# Problem Statement:\nGiven the `head` of a sorted linked list, remove all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.\n\n# Approach\n\n1. Edge Case Handling:\n\n    - If the list is empty (`head == NULL`) or has only one node (`head->next == NULL`), return the head as there are no duplicates to remove.\n\n2. Initialization:\n\n    - `dummy` is a new node created to help handle cases where the first few nodes are duplicates. It points to the head of the list.\n    - `prev` is initialized to point to the `dummy` node. This will be used to keep track of the last node before the sequence of duplicates.\n\n3. Iterate Through the List:\n\n    - `curr` is initialized to the `head` and will be used to traverse the list.\nThe loop continues until `curr` becomes `NULL`, indicating the end of the list.\n\n4. Check for Duplicates:\n\n    - Inside the loop, check if `curr` has a duplicate by comparing `curr->val` with `curr->next->val`.\n    - If duplicates are found, move `curr` forward to skip all nodes with the same value. This is done inside a nested loop.\n\n5. Remove Duplicates:\n\n    - After skipping the duplicates, link `prev->next` to `curr->next`.This effectively removes the sequence of duplicate nodes.\n    - If no duplicates are found, simply move `prev` to `curr`.\n\n6. Advance the Pointer:\n\n    - Move `curr` to the next node in the list.\n\n7. Return the Modified List:\n\n    - Finally, return `dummy->next`, which points to the head of the modified list (excluding the dummy node).\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n`Upvote! It only takes 1 click\uD83D\uDE09`\n# Code\n```\nclass Solution {\npublic:\n    ListNode* deleteDuplicates(ListNode* head) {\n        // Handle edge cases where the list is empty or has only one node\n        if (head == NULL || head->next == NULL) {\n            return head;  // No duplicates to remove, return the list as is\n        }\n\n        // Create a dummy node that points to the head of the list\n        // This helps in handling cases where the first few nodes are duplicates\n        ListNode* dummy = new ListNode(0, head);\n        \n        // Initialize \'prev\' to point to the dummy node\n        // \'prev\' will be used to link nodes that are not part of any duplicate sequence\n        ListNode* prev = dummy;\n        \n        // Initialize \'curr\' to start at the head of the list\n        // \'curr\' will be used to traverse through the list\n        ListNode* curr = head;\n\n        // Traverse through the list until the end\n        while (curr != NULL) {\n            // Check if the current node has a duplicate\n            if (curr->next != NULL && curr->val == curr->next->val) {\n                // Skip all nodes that have the same value as \'curr\'\n                // Move \'curr\' forward until the last duplicate node\n                while (curr->next != NULL && curr->val == curr->next->val) {\n                    curr = curr->next;\n                }\n                // Link \'prev->next\' to the node after the last duplicate\n                // This effectively removes the sequence of duplicates\n                prev->next = curr->next;\n            } else {\n                // If no duplicate is found, move \'prev\' to the current node\n                prev = curr;\n            }\n            // Move \'curr\' to the next node in the list\n            curr = curr->next;\n        }\n\n        // Return the modified list, starting from \'dummy->next\'\n        // We skip the dummy node itself\n        return dummy->next;\n    }\n};\n\n```\n![upvote.jpeg](https://assets.leetcode.com/users/images/48b755ae-c41b-474d-966e-aa5674e41ff4_1720943081.9620614.jpeg)
['Linked List', 'Two Pointers', 'C++']