Simple Solution || Beats 100% || Proper Explanation
\n\n1.The code first checks if the input head is null. If it is, it returns null, indicating an empty list.\nThen, it checks if head.next is null. If it is, it 
\n\n1.The code first checks if the input head is null. If it is, it returns null, indicating an empty list.\nThen, it checks if head.next is null. If it is, it means there\'s only one node in the list, so it returns head as it is (no duplicates to remove).\n\n2.Initialization:\n\nThree ListNode pointers are initialized: prev, current, and next.\nprev is set to null initially.\ncurrent is set to head, which is the first node of the list.\nnext is set to current.next, which is the node next to the current node.\n\n3.Main Loop:\n\nThe code enters a while loop that continues until current.next becomes null. This loop iterates through the list, examining each node.\n4.Check for Duplicates:\n\nInside the loop, the code checks if the value of the current node (current.val) is not equal to the value of the next node (next.val). If they are not equal, it means there\'s no duplicate, and the code proceeds to the next node.\nHandling Duplicates:\n\nIf the values are equal, indicating a duplicate, the code enters the else block.\nInside the else block, there\'s another while loop that iterates while current is not null.\nWithin this loop, the code advances the next pointer until it points to a node with a different value than the current node. This loop effectively skips all consecutive nodes with the same value as the current node.\nAfter exiting this loop, the code updates the pointers to remove the duplicates:\nIf prev is not null, it sets prev.next to current.next, effectively removing all the nodes between prev and current.\nIf prev is null, it means the duplicates were at the beginning of the list, so it updates head to current.next.\nIt then breaks out of the outer while loop to continue iterating through the list.\nReturn Modified List:\n\nAfter traversing the entire list and removing duplicates, the code returns head, which points to the head of the modified list\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    if(head==null){\n        return null;\n     }\n     if(head.next == null) return head;\n    ListNode prev=null;\n    ListNode current=head;\n    ListNode next=current.next;\n    while(current.next!=null){\n        if(current.val!=next.val){\n            prev=current;\n            current=next;\n            if(next!=null){\n                next=next.next;\n            }\n        }\n        else{\n            while(current!=null){\n                next=next.next;\n                if(next==null){\n                    if(prev!=null){\n                        prev.next=null;\n                    }\n                    else{\n                        return null;\n                    }\n                    return head;\n                }\n                if(current.val!=next.val){\n                    current=next;\n                    next=next.next;\n                    if(prev!=null){\n                        prev.next=current;\n                    }\n                    else{\n                        head=current;\n                    }\n                     break;\n                }\n            }\n        }\n    }\n\n        return head;\n    }\n}\n```
['Linked List', 'Two Pointers', 'Java']