Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see multiple things:</p> <p>First of all your last return statement will make the method return the first time you find an element that equals <code>x</code>. You should remove it. This is one cause of error.</p> <p>I think you wanted to use a while loop here so to find every <code>x</code> on the beginning of <code>start</code>?</p> <pre><code>else if (start.data.equals(x)) { start = start.next; count--; } </code></pre> <p>Now look at what happens if <code>start</code> only consists of one element, and this one element is also equal to <code>x</code>? You will receive an exception here</p> <pre><code>for (ptr = start; ptr.next != null; ptr = ptr.next) { </code></pre> <p>as you would call <code>null.next</code> that doesn't exist. Actually it would be even worse with a while-loop, but fortunately a null test will help. ;)</p> <p>Now to more cosmetic things:</p> <p>This</p> <pre><code>if (start == null) return; else if (start.data.equals(x)) { </code></pre> <p>could be written as</p> <pre><code>if (start == null) return; if (start.data.equals(x)) { </code></pre> <p>by omitting the <code>else</code> it becomes a bit easier to read, even though it will behave the same.</p> <hr> <p>This is what I would write:</p> <pre><code>public void eraseNumber(Object x) { while (start != null &amp;&amp; start.data.equals(x) { start = start.next; count--; } Node lastNode = start; Node currentNode = start; while (currentNode != null) { if (currentNode.data.equals(x)) { lastNode.next = currentNode.next; count--; } else { lastNode = currentNode; } currentNode = currentNode.next; } } </code></pre> <p>You may want to substitute <code>Object</code> with something more specific.</p> <p>Note: I had to fix a bug in my implementation missing the first x in the linked list.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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