✅100% BEATS | MADE EASY | STACKS | JAVA | EXPLAINED
Edge case:\n\nCheck if the list is empty (head == null). If so, return null as there are no duplicates to remove.\n\nCreate a dummy node:\n\nInitialize a dummy 
**Edge case:**\n\nCheck if the list is empty (head == null). If so, return null as there are no duplicates to remove.\n\n**Create a dummy node:**\n\nInitialize a dummy node with any value (here, 0). This node simplifies handling cases where the head itself has duplicates.\nSet dummy.next to point to the original head to connect the dummy node to the actual list.\n\n**Initialize pointers:**\n\nInitialize a pointer prev to the dummy node. This pointer will always point to the last distinct node encountered so far.\n\n**Iterate and remove duplicates:**\n\nWhile head is not null:\nCheck if the next node (head.next) exists and has the same value as the current node (head.val). This indicates the beginning of a duplicate sub-list.\nIf duplicates are found:\nIterate a loop using head until you reach the end of the duplicate sub-list (where the value changes).\nUpdate prev.next to point to the node after the duplicate sub-list, effectively skipping all duplicate nodes.\nOtherwise (if no duplicates are found):\nSimply move the prev pointer forward to indicate the current node is distinct.\n\n**Return result:**\n\nSince dummy was used for convenience, return dummy.next as the head of the modified list, which excludes the duplicates.\n\n**Time Complexity: O(n)**\n\nIn the worst case, the code needs to iterate through the entire list (n nodes) once to identify and skip duplicates.\n\n**Space Complexity: O(1)**\n\nThe code uses a constant amount of extra space for the dummy node and pointers, independent of the list length.\n\n# Code\n```java []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n    public ListNode deleteDuplicates(ListNode head) {\n        if (head == null) return null;  // Edge case: If the list is empty, return null\n        \n        ListNode dummy = new ListNode(0);  // Dummy node to handle cases where head has duplicates\n        dummy.next = head;\n        ListNode prev = dummy;  // Pointer to the last node that is guaranteed to be distinct\n        \n        while (head != null) {\n            // If we have found the beginning of duplicates sublist \n            // skip all the nodes in this duplicate sublist\n            if (head.next != null && head.val == head.next.val) {\n                // Move the head pointer to the end of duplicates sublist\n                while (head.next != null && head.val == head.next.val) {\n                    head = head.next;\n                }\n                // Skip all duplicates\n                prev.next = head.next;\n            } else {\n                // Otherwise, move the prev pointer\n                prev = prev.next;\n            }\n            // Move forward\n            head = head.next;\n        }\n        \n        return dummy.next;  // Return the head of the modified list\n    }\n}\n```
['Java']