Easy Solution | Beats 100.00%
Key Concepts:\nDummy Node: A dummy node is added at the start of the linked list to simplify edge cases (e.g., if the head node itself is a duplicate).\nExample
# Key Concepts:\n**Dummy Node**: A dummy node is added at the start of the linked list to simplify edge cases (e.g., if the head node itself is a duplicate).\nExample:\nFor 1 -> 2 -> 2 -> 3, the dummy node creates 0 -> 1 -> 2 -> 2 -> 3.\n\n**Pointer Iteration:** A curr pointer iterates through the list to check for duplicates and skip over them.\n\n**Duplicate Skipping:** If duplicates are found (curr.next.val == curr.next.next.val), all nodes with the duplicate value are skipped by moving a running pointer forward until the value changes.\n\nCode Walkthrough with Visualization:\nLet\'s consider an example list:\n1 -> 2 -> 2 -> 3 -> 4 -> 4 -> 5\n\n**Step-by-Step Execution:\nInitial Setup:**\n\nAdd a dummy node: 0 -> 1 -> 2 -> 2 -> 3 -> 4 -> 4 -> 5.\nSet curr = dummy.\n\n**First Iteration:**\nCheck curr.next.val (1) and curr.next.next.val (2).\nSince 1 != 2, move curr to curr.next.\nList State: 0 -> 1 -> 2 -> 2 -> 3 -> 4 -> 4 -> 5\nPointer Position: curr -> 1.\n\n**Second Iteration:**\nCheck curr.next.val (2) and curr.next.next.val (2).\nSince 2 == 2, duplicates are found.\nMove the running pointer through all nodes with value 2.\nrunning = curr.next (2) -> curr.next.next (2)\nStop when running points to 3.\nUpdate curr.next = running, skipping all 2s.\nList State: 0 -> 1 -> 3 -> 4 -> 4 -> 5\nPointer Position: curr -> 1.\n\n**Third Iteration:**\nCheck curr.next.val (3) and curr.next.next.val (4).\nSince 3 != 4, move curr to curr.next.\nList State: 0 -> 1 -> 3 -> 4 -> 4 -> 5\nPointer Position: curr -> 3.\n\n**Fourth Iteration:**\nCheck curr.next.val (4) and curr.next.next.val (4).\nSince 4 == 4, duplicates are found.\nMove running through all nodes with value 4.\nrunning = curr.next (4) -> curr.next.next (4)\nStop when running points to 5.\nUpdate curr.next = running, skipping all 4s.\nList State: 0 -> 1 -> 3 -> 5\nPointer Position: curr -> 3.\n\n**Final Step:**\nCheck curr.next.val (5) and curr.next.next (None).\nSince there\'s no curr.next.next, exit the loop.\nReturn:\nReturn dummy.next, which points to the head of the modified list:\n1 -> 3 -> 5.\n\n# Complexity\n- Time complexity: O(n)\nWhere n is the number of total nodes in the linkedList\n\n- Space complexity: O(1)\n\n# Code\n```python3 []\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution:\n    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:\n        dummy = ListNode(0, head)\n        curr = dummy\n        while curr.next and curr.next.next:\n            if curr.next.val == curr.next.next.val:\n                running = curr.next\n                num = running.val\n                while running and running.val == num:\n                    running = running.next\n                curr.next = running\n            else:\n                curr = curr.next\n        \n        return dummy.next\n```
['Python3']