Delete duplicate-value nodes from a sorted linked list

  • + 0 comments

    JAVA

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