0ms, 100% Runtime beats, Optimized Two-pointer Approach, Easy to understand
Intuition\nThe main intuition behind this approach is to use two pointers to efficiently traverse the sorted linked list while identifying and removing duplicat
# Intuition\nThe main intuition behind this approach is to use two pointers to efficiently traverse the sorted linked list while identifying and removing duplicate nodes.\n\n# Approach\nInitialization:\n\n->We begin by creating a dummy node with a value of 0. This dummy node is used to handle edge cases at the head of the list, ensuring consistency in our pointer manipulation.\n->We initialize two pointers: prev and current.\nprev points to the node before the current sequence of duplicates.\ncurrent initially points to the head of the list.\n\nTraversal with Two Pointers:\n\n->We iterate through the list using the current pointer, ensuring that we cover every node in the list.\n->Within the outer loop, we check if the current node\'s value is equal to the value of its next node. If they are equal, it indicates the presence of a sequence of duplicate nodes.\n\nHandling Duplicates:\n\n->If duplicates are found, we set the flag variable to true to mark the presence of duplicates. This flag helps us identify if the current sequence of nodes is a duplicate.\n->We move the current pointer until it reaches the last node in the duplicate sequence. This ensures that current points to the last duplicate node.\n->If flag is true, indicating the presence of duplicates, we update prev.next to skip all duplicate nodes by pointing it to the node after the last duplicate node (current.next). This effectively removes the duplicate sequence from the list.\n\nMoving to the Next Node:\n\n->After handling duplicates, we move the current pointer to the next node in the list to continue the traversal.\n\nReturning the Modified List:\n\n->Finally, we return dummy.next, which points to the head of the modified list without duplicates.\n\n# Complexity\n- Time complexity:\nThe time complexity of this approach is O(n), where n is the number of nodes in the linked list. We traverse the list once, and within each iteration, we perform constant-time operations.\n\n\n- Space complexity:\nThe space complexity is O(1) because we only use a constant amount of extra space for the pointers dummy, prev, and current. No additional data structures are used, making this approach memory-efficient.\n\n# Code\n```\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        ListNode dummy = new ListNode(0);\n        dummy.next = head;\n\n        ListNode prev = dummy;\n        ListNode current = head;\n        while(current!=null){\n            boolean flag = false;\n            while(current.next!=null && current.val==current.next.val){\n                current = current.next;\n                flag=true;\n            }\n\n            if(flag){\n                prev.next = current.next;\n            }else{\n                prev = current;\n            }\n\n            current = current.next;\n        }\n        return dummy.next;\n    }\n}\n```
['Java']