Hi. Everyone.
I have a single-linked list and I need to implement an add method. I need to traverse the list (it needs to be in increasing order) and place the parameter object inside. There won't be duplicates.
How do I go about doing this?
public void add(G element) {
Node<G> nodeRef = head;
while(nodeRef != null){
if (nodeRef.data.compareTo(element) < 0){
nodeRef = nodeRef.next;
if (nodeRef.data.compareTo(element) > 0){
Node<G> addNode = new Node<G> ();
addNode.next = nodeRef.next;
nodeRef.next = addNode;
}
}