Delete duplicate-value nodes from a sorted linked list

  • + 0 comments

    Java Solution:

    public static SinglyLinkedListNode removeDuplicates(SinglyLinkedListNode llist) {
        // Write your code here
        if( llist == null ) return null;
        if( llist.next == null ) return llist;
        SinglyLinkedListNode current = llist;
        SinglyLinkedListNode next = llist.next;
        
        while( current.next != null ){
            if( current.data == next.data ){
                current.next = next.next;
                next = next.next;
            }
            else{
                current = current.next;
                next = next.next;
            }
        }
        return llist;
    
        }