Extend the LinkedList class adding a new method printMiddle that prints values of the middle node(s) of a linked list in one pass. Assume that we do not know the size of the linked list ahead of time. Note that if the list has an odd number of nodes, there will be only one middle node; otherwise, there will be two middle nodes. E.g., applying printMiddle to the linked list [1 → 7 → 5 → 4 → 2], we get 5; applying it to [1 → 2 → 3 → 4 → 5 → 6], we get 3 and 4. Extra1 . You may get 5 extra points for each of the problems 1.a, 1.b, and 2 (15 points in total)2 . To this end, you have to explain3 which of the following classes of runtime complexities better characterizes your method – O(log n), O(n), O(n 2 ), or O(2n ). Simply briefly write it in the comments after the method declaration. For example, for the look-up function indexOf, your explanation might look like: public int indexOf(int element) { // O(n): Since we need to iterate over // the entire array to find the number. // This number may be at the end of the array     Use the Java code below to add on please    import java.util.NoSuchElementException; public class LinkedList { private Node head; // first private Node tail; // last private class Node { private int value; private Node next; public Node(int value) { this.value = value; } } private boolean isEmpty() { return (head == null); } private boolean hasNext(Node node) { return (node.next != null); } public void print() { Node current = head; System.out.print("["); while (current != null) { if (hasNext(current)) { System.out.print(current.value + ", "); } else { System.out.print(current.value); } current = current.next; } System.out.println("]"); } public void addFirst(int value) { Node node = new Node(value); if (isEmpty()) { head = tail = node; } else { node.next = head; head = node; } } public void addLast(int value) { Node node = new Node(value); if (isEmpty()) { head = tail = node; } else { tail.next = node; tail = node; } } public int indexOf(int value) { int index = 0; Node current = head; while (current != null) { if (current.value == value) { return index; } index++; current = current.next; } return -1; } public boolean contains(int value) { return (indexOf(value) != -1); } public void deleteFirst() { if (isEmpty()) { throw new NoSuchElementException(); } if (head == tail) { head = tail = null; return; } Node second = head.next; head.next = null; head = second; } public Node previous(Node node) { if (isEmpty()) throw new NoSuchElementException(); Node current = head; while (current.next != node) { if (!hasNext(current)) { throw new NoSuchElementException(); } current = current.next; } return current; } public void deleteLast() { if (isEmpty()) { throw new NoSuchElementException(); } if (head == tail) { head = tail = null; return; } Node lastButOne = previous(tail); lastButOne.next = null; tail = lastButOne; } public int getKthNodeFromTheEnd(int k) { if (isEmpty()) { throw new IllegalStateException(); } if (k <= 0) { throw new IllegalArgumentException(); } Node first = head; Node second = head; for (int i = 0; i < k - 1; i++) { if (!hasNext(first)) { throw new IllegalArgumentException(); } first = first.next; } while (first != tail) { first = first.next; second = second.next; } return second.value; } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Extend the LinkedList class adding a new
method printMiddle that prints values of the middle node(s) of a linked
list in one pass. Assume that we do not know the size of the linked list
ahead of time.
Note that if the list has an odd number of nodes, there will be only
one middle node; otherwise, there will be two middle nodes.
E.g., applying printMiddle to the linked list [1 → 7 → 5 → 4 → 2],
we get 5; applying it to [1 → 2 → 3 → 4 → 5 → 6], we get 3 and 4.
Extra1
. You may get 5 extra points for each of the problems 1.a,
1.b, and 2 (15 points in total)2
. To this end, you have to explain3 which
of the following classes of runtime complexities better characterizes your
method – O(log n), O(n), O(n
2
), or O(2n
). Simply briefly write it in the
comments after the method declaration. For example, for the look-up
function indexOf, your explanation might look like:
public int indexOf(int element) {
// O(n): Since we need to iterate over
// the entire array to find the number.
// This number may be at the end of the array

 

 

Use the Java code below to add on please 

 

import java.util.NoSuchElementException;
public class LinkedList {
private Node head; // first
private Node tail; // last
private class Node {
private int value;
private Node next;
public Node(int value) {
this.value = value;
}
}
private boolean isEmpty() {
return (head == null);
}
private boolean hasNext(Node node) {
return (node.next != null);
}
public void print() {
Node current = head;
System.out.print("[");
while (current != null) {
if (hasNext(current)) {
System.out.print(current.value + ", ");
} else {
System.out.print(current.value);
}
current = current.next;
}
System.out.println("]");
}
public void addFirst(int value) {
Node node = new Node(value);
if (isEmpty()) {
head = tail = node;
} else {
node.next = head;
head = node;
}
}

public void addLast(int value) {
Node node = new Node(value);
if (isEmpty()) {
head = tail = node;
} else {
tail.next = node;
tail = node;
}
}
public int indexOf(int value) {
int index = 0;
Node current = head;
while (current != null) {
if (current.value == value) {
return index;
}
index++;
current = current.next;
}
return -1;
}
public boolean contains(int value) {
return (indexOf(value) != -1);
}
public void deleteFirst() {
if (isEmpty()) {
throw new NoSuchElementException();
}
if (head == tail) {
head = tail = null;
return;
}
Node second = head.next;
head.next = null;
head = second;
}
public Node previous(Node node) {
if (isEmpty())
throw new NoSuchElementException();
Node current = head;
while (current.next != node) {
if (!hasNext(current)) {
throw new NoSuchElementException();
}
current = current.next;

}
return current;
}
public void deleteLast() {
if (isEmpty()) {
throw new NoSuchElementException();
}
if (head == tail) {
head = tail = null;
return;
}
Node lastButOne = previous(tail);
lastButOne.next = null;
tail = lastButOne;
}
public int getKthNodeFromTheEnd(int k) {
if (isEmpty()) {
throw new IllegalStateException();
}
if (k <= 0) {
throw new IllegalArgumentException();
}
Node first = head;
Node second = head;
for (int i = 0; i < k - 1; i++) {
if (!hasNext(first)) {
throw new IllegalArgumentException();
}
first = first.next;
}
while (first != tail) {
first = first.next;
second = second.next;
}
return second.value;
}
}

 

Expert Solution
Step 1

Find the updated code below. 

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education