• + 0 comments

    Java

       public static Node removeDuplicates(Node head) {
             if(head==null||head.next==null){
        return head;
      }
    
      List<Integer> list = new ArrayList<>();
      Node current = head;
      while(current.next!=null){
        list.add(current.data); 
         if(list.contains(current.next.data)){
            // skip the next node
            current.next = current.next.next;                  
            }else {
                current = current.next;
            }         
      }
      return head;
    }