title: Inserting into Sorted LinkedList Java
body: <p>I have this code below where I am inserting a new integer into a sorted LinkedList of ints but I do not think it is the "correct" way of doing things as I know there are singly linkedlist with pointer to the next value and doubly linkedlist with pointers to the next and previous value. I tried to use Nodes to implement the below case but Java is importing this import org.w3c.dom.Node (document object model) so got stuck.</p>
  
  <p>Insertion Cases</p>
  
  <ol>
  <li>Insert into Empty Array</li>
  <li>If value to be inserted less than everything, insert in the beginning.</li>
  <li>If value to be inserted greater than everything, insert in the last.</li>
  <li><p>Could be in between if value less than/greater than certain values in LL.</p>
  
  <pre><code>import java.util.*;
  
  public class MainLinkedList {
  public static void main(String[] args) {
  LinkedList&lt;Integer&gt; llist = new LinkedList&lt;Integer&gt;();
  
  llist.add(10);
  llist.add(30);
  llist.add(50);
  llist.add(60);
  llist.add(90);
  llist.add(1000);
  System.out.println("Old LinkedList " + llist);
  
  //WHat if you want to insert 70 in a sorted LinkedList
  LinkedList&lt;Integer&gt; newllist = insertSortedLL(llist, 70);
  System.out.println("New LinkedList " + newllist);
  }
  
  public static LinkedList&lt;Integer&gt; insertSortedLL(LinkedList&lt;Integer&gt; llist, int value){
  
      llist.add(value);
      Collections.sort(llist);
      return llist;
  
  }
  </code></pre>
  
  <p>}</p></li>
  </ol>