Easy to understand Java solution


  • 0

    '''
    //Set a pointer from the start of the List.
    //If current val equals to the next val then jump it, take care if the next.next exist!

    public ListNode deleteDuplicates(ListNode head) {
    if(head==null||head.next==null) return head;
    ListNode p= head;
    while(p.next!=null){
    if(p.val==p.next.val)
    {
    if(p.next.next!=null)
    { p.next=p.next.next;}
    else{
    p.next=null;
    }
    }else{
    p=p.next;
    }
    }
    return head;
    }
    '''


Log in to reply
 

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