user3792733
user3792733

Reputation: 79

How do I remove the only node in a linked list in java?

I have a while loop that is supposed to run until a certain value is deleted from my linked list.(remainingPoints) The problem is that my compiler gives me an error if that node is the only node in the linked list. edit: I'm using the prewritten linked list class in java.

    while (remainingPoints.contains(endPoint)) {

        //loop through each index of the start points adjacency array and update the path estimates.
        for (int i = 0; i < maxIndex; i++) {
            //if the path estimate is greater than the distance found from the next Point to the
            //i'th point, update the pathEstimate for that point and update the parent of the point.
            if ((pathEstimates[i] != 0 && adjMatrix[next][i] != 0) && (adjMatrix[next][i]+pathEstimates[next] < pathEstimates[i])) {
                pathEstimates[i] = adjMatrix[next][i] + pathEstimates[next];
                parents[i] = next;
            }
        }

        //reset next. 
        next = -1;

        //This will be the intersection that has the shortest path from the last tested 
        //intersection (that is not 0).
        for (int i = 0; i < maxIndex; i++) {
            if (pathEstimates[i] != 0 && remainingPoints.contains(i)) {
                if (next == -1)
                    next = i;
                else if (pathEstimates[i] < next)
                    next = i;
            }
        }

        //Inelegent solution goes here in place of the line of code below:
        remainingPoints.remove(next);

    }

An inelegant solution I tried was to add in this if statement which added a useless node containing -1 so that the node containing next could be deleted making the next iteration of the while loop false, but this added a more curious problem:

    if (remainingPoints.size() == 1)
            remainingPoints.add(-1);
    System.out.println(next);
    System.out.println(remainingPoints.remove(next));

With this attempted solution the loop runs infinitely. The value of next that is printed is 1(this is the correct and intended value of next) but somehow the value of the remainingPoints.remove(next) is -1. I tested other values as well and the value of remainingPoints.remove(next) is always the value that I add using the if statement. This suggests that the the remove method is removing the value I added in which would explain the infinite loop but why is this happening?

If anyone could explain how to simply delete the only node in a linked list in java it would be much appreciated!! A bonus for whoever can explain the above error as well.

As an aside, this is my first post on stack overflow so if I made any posting errors or am ignorant to any stack overflow etiquette please let me know!

Upvotes: 0

Views: 968

Answers (1)

damien hawks
damien hawks

Reputation: 512

Check if the head->next is null or not and if it is then check if the value in the head is the value you are searching for, remove it and return null or else do nothing

You can remove it using this syntax:-

list.remove(0);

Considering that list is your LinkedList and i would also recommend reading Jenkov's tutorials on Lists and collections in general to help you master this. They helped me alot when i started with Java. I've linked them. Please feel free to ask anymore questions and welcome to stack overflow!

Upvotes: 1

Related Questions