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;

                }
            }

You have 99% of the code right there. There's one bug that jumps out at me...
the new node that you add should contain the "element" object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.