title: How to remove an item from a sorted doubly linked list?
body: <p>I have a sorted doubly linked list that always starts with the first element as <code>null</code> and ends with the last element as null. When the list is empty it look like this: <code>{null, null}</code>. When I insert the values <code>​​a, b, c</code>, the list looks like this: <code>{null, a, b, c, null}</code>.</p>
  
  <p>I have a <code>removeFirst()</code> method that should remove the first item from the list and the list should look like this: <code>{null, b, c, null}</code>. And the removed item is returned.</p>
  
  <p>I also have a <code>removeLast()</code> method that removes the last element so the list should look like this: <code>{null, b, null}</code>. And the removed item is returned.</p>
  
  <p>The <code>removeElement()</code> method should remove an item from the list in any place and returns true. If the item is not in the list, it returns false.</p>
  
  <p>The problem with the code I have now is that <code>removeFirst()</code> removes the first item from the list which is <code>null</code>. And I have the same problem with the <code>removeLast()</code> method, it also removes the last element in the list which is null. But the first and last elements in the list should always be null and should not be removed.</p>
  
  <p><code>removeElement()</code> method do not remove any items from the list. I can not see what I'm missing.</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>// removes the first item in the list and returns the string
  public String removeFirst() {
  
      // if the list is empty
      if (this.size == 0) {
          throw new NoSuchElementException();
      }
  
      Node tempNode = first;
  
      // if there is only one item in the list
      if (first.next == null) {
          last = null;
      } else {
          first.next.previous = null;
          first = first.next;
      }
  
      size--;
  
      return tempNode.data;
  }
  
  // removes the last item in the list and returns the string
  public String removeLast() {
  
      // if the list is empty
      if (this.size == 0) {
          throw new NoSuchElementException();
      }
  
      Node tempNode = last.previous;
  
      // if there is only one item in the list
      if (first.equals(last)) {
          first.previous = null;
          first.next = last;
          last.previous = first;
          last.next = null;
  
          tempNode.next = null;
  
      } else {
          last.previous.next = null;
          last = last.previous;
          tempNode.previous = null;
      }
  
      size--;
  
      return tempNode.data;
  }
  
  // removes an item from the list
  public boolean removeElement(String element) {
      boolean removed = false;
  
      try {
          // if the first element in the list should be removed
          if (first.data.equals(element)) {
              // if there is only one item in the list
              if (first.equals(last)) {
                  last = null;
                  first = first.next;
  
              } else {
                  first.next.previous = null;
                  first = first.next;
              }
  
              size--;
              removed = true;
  
              // if the last item in the list should be deleted
          } else if (last.data.equals(element)) {
              last.previous.next = null;
              last = last.previous;
  
              size--;
              removed = true;
  
          } else {
              Node before = first;
              Node after = first.next;
  
              // as long as there are more elements in the list
              while (first.next != null) {
                  if (after.data.compareTo(element) == 0) {
  
                      before.next = after.next;
                      after.next.previous = before.next;
  
                      size--;
                      removed = true;
  
                      break;
                  }
  
                  before = after;
                  after = after.next;
              }
  
          }
      } catch (NullPointerException e) {
  
          removed = false;
      }
  
      return removed;
  }
  </code></pre>