82. Remove Duplicates from Sorted List II with step by step explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Create a dummy node dummy and set its next node as the head of the lin
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Create a dummy node dummy and set its next node as the head of the linked list.\n2. Create a prev variable and initialize it with dummy.\n3. Use a while loop to traverse the linked list while head and head.next are not None.\n4. Check if the value of the current node head is equal to the value of the next node head.next.\n5. If yes, then use another while loop to traverse the linked list and find all the duplicates.\n6. Once all the duplicates are found, set head to head.next and update the next node of prev to head.\n7. If the values are not equal, update the prev to prev.next and head to head.next.\n8. Return dummy.next as the new head of the linked list.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# Definition for singly-linked list.\nclass ListNode:\n    def __init__(self, val=0, next=None):\n        self.val = val\n        self.next = next\n\nclass Solution:\n    def deleteDuplicates(self, head: ListNode) -> ListNode:\n        dummy = ListNode(0)\n        dummy.next = head\n        prev = dummy\n        while head and head.next:\n            if head.val == head.next.val:\n                while head and head.next and head.val == head.next.val:\n                    head = head.next\n                head = head.next\n                prev.next = head\n            else:\n                prev = prev.next\n                head = head.next\n        return dummy.next\n\n```
['Python', 'Python3']