My task is to add the specified element to the linked list if it is not already present
/**
* Adds the specified element to this set if it is not already present.
* @param o element to be added to this set.
* @return true if this set did not already contain the specified element.
* @throws NullPointerException - if the specified element is null and this set does
not support null elements
*/
This is my solution
public boolean add(E o) {
if (o == null)
throw new NullPointerException();
ListNode newNode = new ListNode(o, null);
if (contains(o))
{
return false;
}
else {
head.next=newNode;
head=newNode;
return true;
}
}
Can please anybody check it and correct if there are mistakes, or give hints to correct it?