Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to figure out how to remove the first instance of an integer that is

ID: 3828974 • Letter: I

Question

I am trying to figure out how to remove the first instance of an integer that is inputted from the user. This is what I have so far but I am unsure what to do next or if I am doing anything wrong.

//removes the first occurrence of oldVal in the list
       public void remove(int oldVal)
       {
           IntNode temp = list;
           if(list == null)
               System.out.println("List is empty");
           else if(temp.val != oldVal )
           {
               while(temp.val != oldVal)
               {
                   temp = temp.next;
                  
               }
           }
           else if(temp.val == oldVal)
           {
               while(temp.next != null)
                   temp.next = temp;
              
               temp = temp.prev;
               temp.next = null;
           }
       }

Explanation / Answer

//removes the first occurrence of oldVal in the list
public void remove(int oldVal)
{
IntNode temp = list;
if(list == null)   //If there are no nodes in the list, just prompt it and give up.
System.out.println("List is empty");
else if(temp.val == oldVal)   //If the first node is what you're searching for.
   temp = temp.next;
else
{
    while(temp.next != null && temp.next.val != oldVal)   //Till you either reach the end of list, or the next node value is what you're searching for.
        temp = temp.next;   //Move on to the next node.
    if(temp.next != null)   //If you didn't reached end of node.
        temp.next = temp.next.next;       //So, next node is what you're searching for. Just bypass it.
}  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote