Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First problem:</p> <p>If your for loop is written such that it does not go over the length of the linked list:</p> <p><code>for (int i = 0; i &lt; length(); i++) {</code></p> <p>Then why do you need this?</p> <pre><code> if (node.getNext().getNext() != null) { node= node.getNext(); } </code></pre> <p>In particular, this code might mean that a node just before the end is evaluated twice, because the node beyond it is <code>null</code>. It is not necessarily making your algorithm wrong, but it smells bad and I don't like it.</p> <p>Second problem:</p> <pre><code> for (int j = 0; j &lt; i; j++) { if (node.getInfo().equals(node.getNext().getInfo())) { isDistinct = false; } } </code></pre> <p>This is a faulty way to calculate duplicates because you do not store and advance the position of the second node you are comparing against. Try something like</p> <pre><code> Node node2 = firstNode(); //idk what method gets the first node :) for (int j = 0; j &lt; i; j++) { if (node.getInfo().equals(node2.getInfo())) { isDistinct = false; break; //optimization, stop as soon as we find a duplicate } node2 = node2.GetNext(); } </code></pre> <p>Finally, <strong>whenever faced with code that does not work, attach a debugger, step through it and notice when the code does not do what you expect it to.</strong> This will solve all logic errors quickly with just a small amount of observation. Even if you don't have access to the debugger, you can use <code>print</code> statements to print the value of various variables and when certain code branches execute and compare to what you expected would happen.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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