title: Using a LinkedList And ListIterator to return elements in alphabetical order
body: <p>The function 'addInOrder' is supposed to add String elements (names of Australian cities) in alphabetical order to a LinkedList 'placesToVisit'. </p>
  
  <p>And according to the tutorial I was following, 'addInOrder' function actually worked as intended. However, the function was written in a way to return a boolean, which is <code>private static boolean addInOrder(LinkedList&lt;String&gt; linkedList, String newCity) {}</code>.</p>
  
  <p>But I wanted to experiment and decided to write the function 'addInOrder' to return void instead, which is <code>private static void addInOrder(LinkedList&lt;String&gt; linkedList, String newCity) {}</code>. </p>
  
  <p>But the output did not return in alphabetical order and there were even duplicates despite the use of 'break' keyword in the while loop.</p>
  
  <p>Original code according to the tutorial was:</p>
  
  <pre><code>   private static boolean addInOrder(LinkedList&lt;String&gt; linkedList, String newCity) {
      ListIterator&lt;String&gt; stringListIterator = linkedList.listIterator();
  
          while (stringListIterator.hasNext()) {
              int comparison = stringListIterator.next().compareTo(newCity);
              if (comparison == 0) {
                  System.out.println(newCity + " already listed as destination.");
                  return false;
              } else if (comparison &gt; 0) {
                  stringListIterator.previous();
                  stringListIterator.add(newCity);
                  return true;
              }
          }
          stringListIterator.add(newCity);
          return true;
       }
  </code></pre>
  
  <p>Output: </p>
  
  <pre><code>Now visiting, Adelaide
  Now visiting, Brisbane
  Now visiting, Canberra
  Now visiting, Darwin
  Now visiting, Melbourne
  Now visiting, Perth
  Now visiting, Sydney
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>public class Demo {
  public static void main(String[] args) {
      LinkedList&lt;String&gt; placesToVisit = new LinkedList&lt;&gt;();
      addInOrder(placesToVisit, "Sydney");
      addInOrder(placesToVisit, "Melbourne");
      addInOrder(placesToVisit, "Brisbane");
      addInOrder(placesToVisit, "Perth");
      addInOrder(placesToVisit, "Canberra");
      addInOrder(placesToVisit, "Adelaide");
      addInOrder(placesToVisit, "Darwin");
  
      printList(placesToVisit);
  }
  
  private static void printList(LinkedList&lt;String&gt; linkedList) {
      Iterator&lt;String&gt; i = linkedList.iterator();
  
      while (i.hasNext()) {
          System.out.println("Now visiting, " + i.next());
      }
  
      System.out.println("=============================");
  }
  
  private static void addInOrder(LinkedList&lt;String&gt; linkedList, String newCity) {
      ListIterator&lt;String&gt; stringListIterator = linkedList.listIterator();
  
      if (linkedList.size() == 0) {
          linkedList.add(newCity);
      } else {
          while (stringListIterator.hasNext()) {
              int comparison = stringListIterator.next().compareTo(newCity);
              if (comparison == 0) {
                  System.out.println(newCity + " already listed as destination.");
                  break;
              } else if (comparison &gt; 0) {
                  stringListIterator.previous();
                  stringListIterator.add(newCity);
                  break;
              } else if (comparison &lt; 0) {
                  stringListIterator.add(newCity);
                  break;
              }
          }
          linkedList.addLast(newCity);
      }
  
  }
  } 
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Now visiting, Adelaide
  Now visiting, Darwin
  Now visiting, Brisbane
  Now visiting, Canberra
  Now visiting, Perth
  Now visiting, Melbourne
  Now visiting, Sydney
  Now visiting, Melbourne
  Now visiting, Brisbane
  Now visiting, Perth
  Now visiting, Canberra
  Now visiting, Adelaide
  Now visiting, Darwin
  </code></pre>