To check whether an element in present in a list, we can ``scan'' the list until: -
Iterative implementation:
public static boolean search(ListNode lis, String s) { while (lis != null) { if (lis.info.equals(s)) return true; lis = lis.next; } return false; }
We can also characterize recursively the operation of checking whether an element elem is present in a list lis as follows:
Given such a characterization, the recursive implementation is immediate. We leave it as an exercise.