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: 3828980 • 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)
                System.out.println("List is empty");
            else if(list.val == oldVal ) //head node should be remoced
            {
       list = list.next;
       list.prev = null;              

            }
            else //internal or tail removed
            {
                while(temp.next != null) {
                    if (temp.next.val == oldVal) //mark the node just belore the target node
           break;
            temp = temp.next;
                }
       if (temp.next == null) { //it target node is null, it is not found
           System.out.println("Old value is not present");
           return;
       }
                temp.next = temp.next.next; //juggle with the pointers. Little knowledge on LinkedList will help you
                temp.next.prev = temp;
            }
        }

Without the actual data structure, it is really tough to answer such questions. Still this is my hunch on the code. I kept i simple, and also commented the code to make life easy. If incase you face come issue with the code, plese feel free to comment below.