[Python3] Simple recursive and iterative solution (with figure explanation)
Recursive\n- Idea\n\t- Base case: list is empty or contains only one element --> No duplicates\n\t- Recursive case: Assuming that the rest of the list is alread
**Recursive**\n- Idea\n\t- Base case: list is empty or contains only one element --> No duplicates\n\t- Recursive case: Assuming that the rest of the list is already done removing duplicates, we only need to consider the first part\n\t\t- There\'s no duplicate in the first part\n\t\t\t![image](https://assets.leetcode.com/users/images/490adbec-efbb-4f4e-87aa-feff28e37f78_1618746910.08464.png)\n\t\t- There\'re duplicates in the first part\n\t\t\t![image](https://assets.leetcode.com/users/images/7e0078bb-8da8-4ffe-a3ef-4f1c6748603f_1618747062.235478.png)\n- Implementation\n   ```python\n   def deleteDuplicates(self, head: ListNode) -> ListNode:\n        # Base case\n        if not head or not head.next:\n            return head\n        \n        # Recursive case\n        \n        # No duplicate in the first part\n        if head.next.val != head.val: \n            head.next = self.deleteDuplicates(head.next)\n            return head\n        \n        # Duplicates exist in the first part\n        cur = head\n        while cur.next and cur.next.val == cur.val:\n            cur = cur.next\n        return self.deleteDuplicates(cur.next)\n   ```\n   \n- Complexity\n\t- Time: O(n)\n\t- Space: O(n)\n\n\n\n**Iterative**\n- Idea\n\t- Add `dummy_head` before `head`. `dummy.next` is the first node of the list.\n\t\t- Using `dummy_head` can help us to handle edge cases easily. For example, removing duplicate nodes at the beginning of list.\n\t- Use two pointers `prev` and `cur` for removing nodes\n\t- Iterate the list\n\t\t- If there\'s no duplicate, move `prev` and `cur` one step forward\n\t\t- If there\'re duplicate\n\t\t\t- Iterate `cur` to the last duplicate node\n\t\t\t- "Jump over" the duplicates \n\t\t\t\t- `prev.next = cur.next` \n\t\t\t\t- `cur = cur.next`\n\t\t![image](https://assets.leetcode.com/users/images/b1eff2b9-24b8-47ac-9b09-88341cc0db64_1618752302.197709.png)\n- Implementation\n\t```python\n\tdef deleteDuplicates(self, head: ListNode) -> ListNode:\n        dummy_head = ListNode(next=head)\n        prev, cur = dummy_head, head\n        \n        while cur and cur.next:\n            if cur.val != cur.next.val:\n                # If there\'s no duplicate,\n                # move prev and cur one step forward\n                prev, cur = cur, cur.next\n            else:\n                # If there\'re duplicates,\n                # iterate cur to the last duplicate nodes,\n                while cur.next and cur.val == cur.next.val:\n                    cur = cur.next\n                    \n                # and jump over the duplicates\n                prev.next = cur.next\n                cur = cur.next\n        \n        return dummy_head.next\n\t```\n\t\n- Complexity\n\t- Time: O(n)\n\t- Space: O(1)\n\n\nIf you think this is helpful, please give it a vote.
['Recursion', 'Iterator', 'Python']