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

user3792733

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!

damien hawks

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!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I remove the first node in a linked list with a remove method

From Dev

How do I add an object to a linked list in alphabetical order in Java?

From Dev

How do I search through a Java Linked List

From Dev

How do I remove only one instance from a list in Python?

From Dev

How to remove all evens in linked list java

From Dev

How to remove an object from a linked list in java

From Dev

How to append a node in a doubly linked list in Java?

From Dev

How to append a node in a doubly linked list in Java?

From Dev

How do I add a linked document to a linked list using the Java API for OrientDB?

From Dev

Remove node from a linked list

From Dev

Deleting the only node of a linked list

From Dev

How do I reverse through a linked list

From Dev

How do I use this Linked List implementation?

From Dev

How do I Sort the names in a Linked List?

From Dev

Circular linked list: How to correct my remove node function?

From Dev

How do I delete a node at the end of a linked list without relying on methods?

From Dev

How do I store a linked list's head node's address in a file and retrieve it later

From Dev

How can I implement a linked list in java?

From Dev

Remove all linked list in Java

From Dev

Remove duplicates in a linked list in java

From Dev

How can I remove a randomly chosen element from a linked list?

From Dev

How do I remove parent node for output and display only children without the parent?

From Dev

How do I remove whole sub-list from list, if i have only few element of the sub-list? python

From Dev

How do I remove only shopping searches?

From Dev

How do I remove only shopping searches?

From Dev

How does remove tail from singly Linked List in Java work

From Dev

How this Java code works? Remove duplicates from an unsorted linked list

From Dev

How does remove tail from singly Linked List in Java work

From Dev

Remove node from Singly Linked List

Related Related

  1. 1

    How do I remove the first node in a linked list with a remove method

  2. 2

    How do I add an object to a linked list in alphabetical order in Java?

  3. 3

    How do I search through a Java Linked List

  4. 4

    How do I remove only one instance from a list in Python?

  5. 5

    How to remove all evens in linked list java

  6. 6

    How to remove an object from a linked list in java

  7. 7

    How to append a node in a doubly linked list in Java?

  8. 8

    How to append a node in a doubly linked list in Java?

  9. 9

    How do I add a linked document to a linked list using the Java API for OrientDB?

  10. 10

    Remove node from a linked list

  11. 11

    Deleting the only node of a linked list

  12. 12

    How do I reverse through a linked list

  13. 13

    How do I use this Linked List implementation?

  14. 14

    How do I Sort the names in a Linked List?

  15. 15

    Circular linked list: How to correct my remove node function?

  16. 16

    How do I delete a node at the end of a linked list without relying on methods?

  17. 17

    How do I store a linked list's head node's address in a file and retrieve it later

  18. 18

    How can I implement a linked list in java?

  19. 19

    Remove all linked list in Java

  20. 20

    Remove duplicates in a linked list in java

  21. 21

    How can I remove a randomly chosen element from a linked list?

  22. 22

    How do I remove parent node for output and display only children without the parent?

  23. 23

    How do I remove whole sub-list from list, if i have only few element of the sub-list? python

  24. 24

    How do I remove only shopping searches?

  25. 25

    How do I remove only shopping searches?

  26. 26

    How does remove tail from singly Linked List in Java work

  27. 27

    How this Java code works? Remove duplicates from an unsorted linked list

  28. 28

    How does remove tail from singly Linked List in Java work

  29. 29

    Remove node from Singly Linked List

HotTag

Archive