title: How does the insert method work in a linked list?
body: <p>I'm trying to implement a linked list using three classes <code>B</code>, <code>IntSet</code>, and <code>Node</code>.</p>
  
  <p>Below is the code from my solution book. I need to know two things:</p>
  
  <ol>
  <li>In class <code>B</code> I do not know what to write in the <code>main</code> method to run my code.</li>
  <li>I need someone to describe me the <code>insert</code> method in the class <code>B</code>. I do understand the first <code>if</code> statement in the <code>insert</code> method, but I do not understand the other <code>if</code> statements in the <code>insert method</code>.</li>
  </ol>
  
  <p>Here are the three classes:</p>
  
  <pre><code>package person;
  
  public class B implements IntSet {
      private Node first;
      private int size;
  
      public static void main(String[] args) {
      }
  
      public B() {
          first = new Node(Integer.MIN_VALUE, Integer.MIN_VALUE);
          first.next = new Node(Integer.MAX_VALUE, Integer.MAX_VALUE);
      }
  
      @Override
      public boolean contains(int nbr) {
          // TODO Auto-generated method stub
          Node p = first;
          while (p != null) {
              if (p.min &lt;= nbr &amp;&amp; p.max &gt;= nbr) {
                  return true;
              }
              if (p.min &gt; nbr) {
                  return false;
              }
              p = p.next;
          }
          return false;
      }
  
      @Override
      public boolean insert(int nbr) {
          // TODO Auto-generated method stub
          Node p = first;
          while (p != null) {
              if (p.min &lt;= nbr &amp;&amp; p.max &gt;= nbr) {
                  return false;
              }
              if (p.max + 1 == nbr &amp;&amp; p.next.min - 1 == nbr) {
                  p.max = p.next.max;
                  p.next = p.next.next;
                  break;
              } else if (p.max + 1 == nbr) {
                  p.max = nbr;
                  break;
              } else if (p.next.min - 1 == nbr) {
                  p.next.min = nbr;
                  break;
              } else if (p.max &lt; nbr &amp;&amp; p.next.min &gt; nbr) {
                  Node n = new Node(nbr, nbr);
                  n.next = p.next;
                  p.next = n;
                  break;
              }
              p = p.next;
          }
          size++;
          return true;
      }
  
      @Override
      public int size() {
          // TODO Auto-generated method stub
          return size;
      }
  }
  
  package person;
  
  public interface IntSet {
      /** Tar reda på om nbr finns i mängden. */
      boolean contains(int nbr);
  
      /**
       * Sätter in nbr i mängden om det inte redan finns. Returnerar true om nbr
       * kan sättas in i mängden, i annat fall false.
       */
      boolean insert(int nbr);
  
      /* Tar reda på antal tal i mängden. */
      public int size();
  }
  
  package person;
  
  class Node {
      int min;
      int max;
      Node next;
  
      Node(int min, int max) {
          this.min = min;
          this.max = max;
          next = null;
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/jBdCP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jBdCP.png" alt="enter image description here"></a></p>