title: Linked List Delete Method
body: <p>The following is a class definition for my Linked List.  I run a test program that creates a new LinkedList and inserts the numbers "3, 2, 1" and then prints the list.  This works fine.  However, when I try and delete "3" or "2," the delete method never completes.  When I try and delete "1," it just prints out the complete list as if nothing had been deleted.</p>
  
  <pre><code>public class LinkedListTest implements LinkedList {
  private Node head;
  
  public LinkedListTest(){
      head = new Node();
  }
  
  public void insert(Object x){
      if (lookup(x).equals(false)){
  
          if (head.data == null)
              head.data = x;
  
          else{
              //InsertLast
              Node temp = head;
  
              while (temp.next != null){
                  temp = temp.next;
              }
  
              Node NewNode = new Node();
              NewNode.data = x;
              NewNode.next = null;
  
              temp.next = NewNode;
  
          }
      }
      //Runtime of insert method will be n, where n is the number of nodes
  }
  
  public void delete(Object x){
      if (lookup(x).equals(true)){
          if (head.data == x)
              head = head.next;
  
          else{
              Node temp = head;
              while (temp.next != null){
                  if ((temp.next).data == x)
                      temp.next = (temp.next).next;
                  else
                      temp = temp.next;
              }
          }
  
      }
  }
  
  public Object lookup(Object x){
      Node temp = head;
      Boolean search = false;
  
      if (head.data == x)
          search = true;
  
      while (temp.next != null){
          if (temp.data == x){
              search = true;
          }
  
          else{
              temp = temp.next;
          }
      }
  
      return search;
  }
  
  public boolean isEmpty(){
      if (head.next == null &amp;&amp; head.data == null)
          return true;
      else
          return false;
  }
  
  public void printList(){
      Node temp = head;
      System.out.print(temp.data + " ");
  
      while (temp.next != null){
          temp = temp.next;
          System.out.print(temp.data + " ");
      }
  
  }
  }
  </code></pre>
  
  <p>EDIT: Here is the node class:</p>
  
  <pre><code>public class Node {
  public Object data;
  public Node next;
  
  public Node(){
      this.data = null;
      this.next = null;
  }
  }
  </code></pre>