title: Adding in a Sorted Linked List
body: <p>So I am writing a code for a Sorted LinkedList that stores integers. Integers must be stored in ascending order when added. My code for a Node class has two values and constructors that are not written here because they are obvious. </p>
  
  <pre><code>public class Node {
  int value;
  Node next;
  }
  </code></pre>
  
  <p>Part of my code for Sorted Linked List is </p>
  
  <pre><code>public class SortedList {
  
  Node head;
  public int listCount;
  
  
  public SortedList(){
      listCount = 0;
      this.head=null;
  
  }
  
  
  
  public void add(int num){
      Node newNode= new Node(num);
      Node temp=head;
  
      if (head==null) {
          head=newNode;
          listCount++;
          System.out.println("Node with data "+num+" was added.");
      }
       else {
          while ((temp.value &lt; num) &amp;&amp; (temp.next!=null)) {
              temp = temp.next;
          }
          if (temp.next==null){
              temp.next=newNode;
              listCount++;
              System.out.println("Node with data "+num+" was added.");
          }
          else {
              newNode.next=temp.next.next;
              temp.next=newNode;
              listCount++;
              System.out.println("Node with data "+num+" was added.");
          }
  
  
      }
  
  }
  </code></pre>
  
  <p>So when I test my code and add numbers 1,81,63,7,8,9,23 my output is </p>
  
  <blockquote>
    <p>Node with data 1 was added.Node with data 81 was added.
         Node with data 63 was added.
         Node with data 7 was added.
         Node with data 8 was added.
         Node with data 9 was added.
         Node with data 23 was added.
         1 -> 81 -> 23 END List Count is 7</p>
  </blockquote>
  
  <p>So you can see that when I try to show the linked list it is only 1 that points to 81, that points to 23. All other values where lost. 
  I used this code to get output:</p>
  
  <pre><code>      public String toString(){
          Node temp = head;
          while(temp.next!=null){
              System.out.print(temp.value+" -&gt; ");
              temp = temp.next;
          }
          System.out.print(temp.value);
          return " END List Count is "+listCount;
  
  }
  </code></pre>