Submit this homework using Gradescope. You can type up your answers, or write legibly and scan them. Do not attempt to submit a paper copy in class, as it will not be accepted.
Suppose you have a linked list composed of Node
s:
public class Node<E> {
public Node<E> next;
public E data;
}
(1 point) Suppose you want to find the i’th node in a list of nodes, or null
if there is no such node. Write a recursive definition (in written prose, not the Java code) for getNode
, in the style from lecture. That is, identify what the base case(s) are and what they should return, and identify the recursive case and what it should do in terms of the current node and the “rest” of the list.
(1 point) Write a recursive method public static <E> Node<E> getNode(Node<E> node, int i)
that returns the i’th node in the linked list starting at node, or null if the given index is out of bounds. Assume a non-negative integer input for i
.
This method must be recursive, the usage of iteration (for
or while
) will earn you no points!
(1 point) Implement public static boolean contains(Node<Integer> node, Integer e)
using recursion. That is, it will return true
iff the linked list of integers contains the given integer. Once again, no explicit loop constructs (for
, while
) allowed!