Simple Java solution using two pointers


  • 0
    R
    public ListNode deleteDuplicates(ListNode head) {
    if(head==null)
        return null;
        ListNode prev=head;
        ListNode curr=null;
        if(head.next!=null){
            curr=head.next;
        }
        while(curr!=null){
            if(prev.val==curr.val){
                prev.next=curr.next;
                curr=prev.next;
            }
            else{
                prev=prev.next;
                curr=curr.next;
            }
        }
        return head;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.