Good evening
I'm trying to implement a single linked list by myself and I've run into an issue when I want to create a search method. Evidently when you want to search for a node (which will be used to insert a node at a certain place) you will have to evaluate some values to see if you reached the right spot. Considering my nodes only have a data field as a identifier, I don't see any other way than using that. However, since the data field isn't unique there might be multiple nodes eligible.
Consider the following list: 5, 7, 2, 8, 3, 1, 6, 5, 8, 4, 2. When I want to add a node somewhere in the list (say: After the node with value 8) he will go trough the list and add the new node after the first occurrence of '8'. What should I do if I wanted to insert it after the 2nd 8?
Is this even possible with a Single Linked List?
Other than that I'd like to have some feedback on my 'removeLast()' method which doesn't seem to do what I want it to do (remove the last node from the list). I am aware my code isn't supposed to work if the list has only 1 value, I'll look into that as soon as the general code of removing the last node works.
My code can be found here.
Edited with code:
public class SingleLinkedList {
public void deleteLast() {
if (lastNode != null) {
Node currentNode = firstNode;
while (currentNode != null) {
Node nextNode = currentNode.getNextNode();
Node nextNextNode = nextNode.getNextNode();
if (nextNextNode == null) {
nextNextNode = null;
lastNode = nextNode;
}
}
listSize--;
}
}
}