In JAVA Recurrsion Methods For exercises 16 to 19 assume we have a sorted linked
ID: 3796367 • Letter: I
Question
In JAVA
Recurrsion Methods
For exercises 16 to 19 assume we have a sorted linked list of Integer. The start of the linked list is referenced by values which, of course, is of type LLNode<Integer>. For example purposes, assume values points to a list containing 3, 6, 6, 9, 12, 15, 18, 19, 19, and 20. You can and should assume the list in question is sorted in nondecreasing order. For each of these exercises you should also create a driver application that demonstrates that your new method operates correctly.
18.Write a recursive method remove(int target, LLNode<Integer> list) that removes all occurrences of target from list and returns a reference to the new list. For our example list the statement
values = remove(6, values);
would result in values referencing the list containing 3 9 12 15 18 19 19 and 20. If target is not contained in list then the list remains unchanged.
Explanation / Answer
Hi, Please find my code.
public static LLNode<Integer> remove(int target, LLNode<Integer> list)
{
if (list != null) // if list is not null then
if (list.getData() == target) if currnet node has the value =target, then remove this
list = remove(target, list.getNext());
else
list.next = remove(target, list.getNext());
return list;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.