Question: Consider the following Singly LinkedList java codes public class Nodel private Object element; private Node next; ... // the constructor, methods setElemento, setNext().getElement().getNext() are omitted
Consider the following Singly LinkedList java codes public class Nodel private Object element; private Node next; ... // the constructor, methods setElemento, setNext().getElement().getNext() are omitted public class SLLE private Node head: ... // the constructor, methods setHead().getHead(), add(), remove() are omitted Write a method in class SLL to find the middle element. Specifically, 1. The method returns null if the Singly LinkedList is empty; 2. The method returns the nt. th node if the Singly Linkedlist has n nodes, whele n is an odd number; 3. The method returns the th node if the Singly Linkedlist has n nodes, where n is an even number. Please show your idea and the java code. Use comments to make your java code easy to understand. Remark: You can only call the method getNext(). You cannot call other methods. This is to prevent the following trick: assume there is a method "miracle" in SLL which can do exactly what describes in the question. If so, then the answer would be simply public Node middle(){ return miracle(:)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
