I’m trying to create a function that takes a linked list and searches through each node in the list. If the node has a matched int, i1, then exchange with i2. Finally return a linked list that is a copy of its substitution.
Iteratively, I think I could just create a copy of the list, iterate through each node, and then check and replace.
for(LN copy = list; copy != null; copy = copy.next;)
if (copy.value == i1)
copy.value == i2;
return copy;
The prototype is
public static LN copySwap (LN list, int i1, int i2);
I’m not sure how I could begin to implement this recursively.
Assuming the constructor for
LNtakes a value and the next node in the list, this should work: