title: Implementing a linked list in Java (Finding/Deleting Nodes)
body: <p>I've been working on some code to implement an (edit: <em>unidirectional</em>) linked list in java. My main issue is coming about when it comes to finding and deleting nodes, that is, using Find(data) tells the user whether or not a node is present in the list; Delete(data) actually removes the node from the list after it is found (or does nothing if the node isn't found). Find(data) and Delete(data) use the same if-else logic, so for right now I just copied the code from the Find method into the delete method, with appropriate pointers in the delete method to "hop over" the deleted node.</p>
  
  <p>I'm wondering if there's a more efficient way of doing this. I thought of using booleans in the delete block, for example: </p>
  
  <pre><code>public void Delete(data)
  {
      if Find(data) 
      { 
          //code to delete node
      }
  }
  </code></pre>
  
  <p>but because the current node could be at the head, tail, or somewhere in the middle, you'd still have to have the loop logic to check where you are so you can set the appropriate references. For example, if the user wants to delete the node at the tail, then the previous node's next node would be set to null. However, in the middle, you'd have to run a while loop to iterate through the list, i.e.</p>
  
  <pre><code>while (iter.NextNode !=null)
          {
              if (iter.NextNode.data == data)
              {
                  iter.NextNode = iter.NextNode.NextNode;
                  System.out.println(data + " was found in the list and removed"
  
                  break;
  
              }
              else if (iter.NextNode.NextNode == null &amp;&amp; iter.NextNode.data == data) 
              {//this is kinda weird. I couldn't use an else block, because either 
               //the code would never exit the loop, or it would keep running
                  iter.NextNode = null;
                  System.out.println(data + " was found in the list and removed. ");
                  break;
              }
  
          iter = iter.NextNode;
  
          if (node.NextNode == null &amp;&amp; node.data != data) 
          {//i guess I could have just put this if statement inside
           //the else block
              System.out.println(data + " was not found in the list.");
              break;
          }
  
  
          }
  </code></pre>
  
  <p>The above code block handles both cases. </p>
  
  <p>The below code block is my Find(data) method:</p>
  
  <pre><code>public void Find(int data)
  {
      Node node = head;
      if (head == null)
      {
          System.out.println("No nodes found. ");
  
      }
  
  
      else if (node.NextNode==null)
      {
          if (node.data == data)
          {
              System.out.println( data + " was found in the list.");
  
          }
          else
          {
              System.out.println("That value was not found in the list.");
  
          }
      }
      else
      {
          while (node.NextNode !=null)
          {
              if (node.data == data)
              {
                  System.out.println(data + " was found in the list.");
                  break;
  
              }
              else if (node.NextNode.NextNode == null &amp;&amp; node.NextNode.data == data)
              {
                  System.out.println(data + " was found in the list.");
                  break;
              }
              else    
              {
                  node = node.NextNode;
              }
              if (node.NextNode == null &amp;&amp; node.data != data)
              {
                  System.out.println(data + " was not found in the list.");
                  break;
              }
          }
  
      }
  </code></pre>
  
  <p>}</p>
  
  <p>In case the question wasn't clear: Is there a way I can use my Find(data) method in the delete block, and take out all the logic?</p>
  
  <p>Thanks for your guidance. I really appreciate it!</p>