Java Solution with Comments


#1
public class Solution {
    public ListNode deleteDuplicates(ListNode A) {
        // if no list or only one node present
        if(A == null || A.next == null) return A;
        ListNode temp = A; // temp node - previous node of the current node
        ListNode cur = temp.next; // cur node - current node
        while(cur != null) { // while current node is not null
            if(temp.val == cur.val) {
                // if previous node has same value as current node then
                // remove current node and point previous node to the next of current node
                // and update currrent node to the next of it.
                temp.next = cur.next;
                cur = temp.next;
            } else {
                // else increment previous and current node
                temp = cur;
                cur = cur.next;
            }
        }
        return A;
    }
}