Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd a node in a single linked list
    text
    copied!<p>Good evening</p> <p>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.</p> <p>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? </p> <p>Is this even possible with a Single Linked List?</p> <p>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.</p> <p>My code can be found <a href="https://github.com/Vannevelj/Exercises/tree/master/Exercises/src" rel="nofollow">here</a>.</p> <p>Edited with code:</p> <pre><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--; } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload