I have a code to add a node...what would be a code to remove it if i provide the coeff

public void addNode(int coeff, int power)
    {
        if (head == null) // start the list
        {
            head = new Node(coeff, power, null);
            degree = power;
        }
        else
        {
            Node n = head;
            Node last = null;
            
            while (n != null)
            {
                if (power > n.power) // insert in list
                {
                    if (last == null)
                        head = new Node(coeff, power, n);
                    else
                        last.next = new Node(coeff, power, n);
                    degree = power;
                    break;
                }
                last = n;
                n = n.next;
            }
            if (n == null) // append to list
            {
                last.next = new Node(coeff, power, null);
            }
        }
    }

Considering that there is a class available that will do this for you, LinkedList, I'm assuming this is some sort of assignment? (Which is fine)


Anyway... pseudocode to do this


-Given the index you want to remove
-set the node at the previous index to point to the node at the next index
-set the index you want to remove as pointing to nothing, this includes setting its previous and next nodes to null if you are in a doubly linked list. If its a singly linked list, then you only have to set next to null
-Java does garbage collection for you, so you're done.

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.