Write a code that delete all occurrences of a given key in a linked list using t
ID: 3907320 • Letter: W
Question
Write a code that delete all occurrences of a given key in a linked list using the code given that only removes a single instance. (Java) thank you!
public void sortedRemove(Object item) throws NoSuchElementException //find node to specified item SortedNode SortedNode while(curr prevnull: curr-head; !-null && ((comparable) item) . compareTo(curr . ?tem) > e){ prev-curr curr curr.next; if(curr if(prevnull) //if found at head remove head else //remove appropriate node except head numberOfItems-//then decrement list numitems else //throw execption null && ((Comparable)item).compareTo(curr.item) -e) head -head.next; prev.next-curr.next; throw new NoSuchElementException(item "is not found!");Explanation / Answer
void deleteAllOccurences(Object item) {
SortedNode curr = head;
SortedNode temp = curr;
while( curr != null ) {
try{
temp = curr.next;
sortedRemove(curr.item);
curr = temp;
}catch(Exception e){
curr = curr.next;
}
}
}
Thanks, PLEASE UPVOTE if helpful
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.