title: How does insert and delete method of SortedList in Java work
body: <p>This is more about my lack of ability to understand the SortedList Class. The ListNode I understand: it is a node made up of 2 things: A comparable object and a ListNode object which itself has the same 2 things.</p>
  
  <p>I do not understand how the insert and delete method work in the SortedList Class down below.
  It's very complex to understand. In the SortedList Class, if the head which is null, points to curr. Shouldn't curr be null? Then the while loop should not execute, but it in fact does execute when I use println to test execution. </p>
  
  <p>Code for ListNode Class:</p>
  
  <pre><code>public class ListNode {
  
      private Comparable data;
      private ListNode next;
  
      public ListNode() {
          data = null;
          next = null;
      }
  
      public ListNode(Comparable x) {
          data = x;
          next = null;
      }
  
      public ListNode(Comparable x, ListNode nextListNode) {
          data = x;
          next = nextListNode;
      }
  
      public Comparable getData() {
          return data;
      }
  
      public ListNode getNext() {
          return next;
      }
  
      public void setData(Comparable x) {
          data = x;
      }
  
      public void setNext(ListNode nextListNode) {
          next = nextListNode;
      }
  
  }
  </code></pre>
  
  <p>Code for SortedList:</p>
  
  <pre><code>public class SortedList {
  
      private ListNode head;
  
      public SortedList() {
          head = null;
      }
  
      public void insert(Comparable x) {
          ListNode curr = head, prev = null;
          while (curr != null &amp;&amp; x.compareTo(curr.getData()) &gt; 0) {
              prev = curr;
              curr = curr.getNext();
          }
          if (prev == null) {
              head = new ListNode(x, curr);
          } else {
              prev.setNext(new ListNode(x, curr));
          }
      }
  
      public boolean delete(Comparable x) {
          if (head == null) {
              return false;
          }
          ListNode curr = head, prev = null;
          while (curr != null &amp;&amp; x.compareTo(curr.getData()) &gt; 0) {
              prev = curr;
              curr = curr.getNext();
          }
          if (curr == null || x.compareTo(curr.getData()) != 0) {
              return false;
          }
          if (prev == null) {
              head = curr.getNext();
          } else {
              prev.setNext(curr.getNext());
          }
          return true;
      }
  
      public void output() {
          for (ListNode curr = head; curr != null; curr = curr.getNext()) {
              System.out.println(curr.getData());
          }
      }
  
  }
  </code></pre>
  
  <p>Code for TestSortedList:</p>
  
  <pre><code>public class TestSortedList {
  
      public static void main(String[] args) {
          SortedList list = new SortedList();
          list.insert(5);
          list.insert(1);
          list.insert(10);
          list.insert(3);
          list.insert(7);
          list.output();
          System.out.println(list.delete(5));
          System.out.println(list.delete(5));
          System.out.println(list.delete(1));
          System.out.println(list.delete(10));
          System.out.println(list.delete(11));
      }
  
  }
  </code></pre>