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

In class LinkedList, create a method called replace which will: a) find a node w

ID: 3565478 • Letter: I

Question

In class LinkedList, create a method called replace which will: a) find a node whose value matches the first value passed as a parameter b) remove that node c) create a new node, with the second value that was passed as a parameter d) insert that new node in that same place in the linked list where the other node was removed

This replace method inserts the new node in the same place that the other node was deleted, however that might not be the correct place for this node (if we are keeping the list sorted). As a result you cannot call methods delete or insert here.

thi is what i got so far. help me correct it.

public void replace(int b; int c){
  
               if( b == head.getData()){
                   head = head.getNext();
                   head = new Node(c, head);
                  
               }else{
                   // Node previous = head;
                   // Node current = head.getNext();
                   while(current != null){
                       if(b == current.getData()){
                           previous.setNext(current.getNext());
                       }else{
                           previous = current;
                           current = current.getNext();
                           previous.setNext(new Node(c, current));
                          
                           break;
                       }
                   }// end of while.
                   if(current == null){
                       previous.setNext(new Node(c));// insert at the end.
                   }
               }
           }

          
       }
          

Explanation / Answer

if( b == head.getData()){
                   head = head.getNext();
                   head = new Node(c, head);
                  
               }

else{
Node previous = head;
Node current = head.getNext();
                   while(current != null)

{
                       if(b == current.getData())

{
                           current.getData()=c;
}

else

{
                           previous = current;
                           current = current.getNext();
}
                   }// end of while.
                   if(current == null)

{
                       previous.setNext(new Node(c));// insert at the end.
                   }
               }
           }

          
       }

here we are trying to change the value of b to c; with removing the node;

after that we can use sorting method to sort, if we are keeping the list sorted.
  

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