Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following Singly LinkedList java codes public class Node{ private Object element; private Node next; // the constructor, methods setElement(), setNext(), getElement(), getNext() are

Consider the following Singly LinkedList java codes

public class Node{

private Object element;

private Node next;

// the constructor, methods setElement(), setNext(), getElement(), getNext() are omitted

}

public class SLL {

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 image text in transcribed th node if the Singly LinkedList has n nodes, where n is an odd number;
  3. The method returns the image text in transcribed 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(); }.

n+1 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Robb,Carlos Coronel

5th Edition

061906269X, 9780619062699

More Books

Students also viewed these Databases questions

Question

l Describe two security benefits.

Answered: 1 week ago

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago