import java.util.NoSuchElementException; class LinkedList { private Node head; private Node tail; 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 i = 0; Node current = head; while (current != null) { if (current.value == value) { return i; } i++; current = current.next; } return -1; } public boolean contains(int value) { return (indexOf(value) != -1); } public void deleteFirst() throws NoSuchElementException { if (isEmpty()) { throw new NoSuchElementException(); } if (head == tail) { head = tail = null; return; } Node formerHeadNext = head.next; head.next = null; head = formerHeadNext; } public Node previous(Node node) throws NoSuchElementException { 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() throws NoSuchElementException { if (isEmpty()) throw new NoSuchElementException(); if (head == tail) { head = tail = null; return; } Node lastButOne = previous(tail); lastButOne.next = null; tail = lastButOne; } } public class LinkedListFromScratch { public static void main(String[] args) { LinkedList myList = new LinkedList(); myList.addLast(2); myList.addLast(4); myList.addLast(8); myList.addFirst(-2); myList.addFirst(-4); myList.addLast(9); myList.print(); System.out.println(myList.indexOf(4)); System.out.println(myList.contains(9)); for (int i = 0; i < 6; ++i) { myList.deleteLast(); myList.print(); } } } In this linked list Add in an O(n) method that reverse to the class LinkedList created in the problem 1. This method must reverse the order of the nodes in the list. For example, having applied reverse to the list [2 → 4 → 8 → 9 → 8], we will change it to [8 → 9 → 8 → 4 → 2].
import java.util.NoSuchElementException;
class LinkedList {
private Node head;
private Node tail;
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 i = 0;
Node current = head;
while (current != null) {
if (current.value == value) {
return i;
}
i++;
current = current.next;
}
return -1;
}
public boolean contains(int value) {
return (indexOf(value) != -1);
}
public void deleteFirst() throws NoSuchElementException {
if (isEmpty()) {
throw new NoSuchElementException();
}
if (head == tail) {
head = tail = null;
return;
}
Node formerHeadNext = head.next;
head.next = null;
head = formerHeadNext;
}
public Node previous(Node node) throws NoSuchElementException {
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() throws NoSuchElementException {
if (isEmpty())
throw new NoSuchElementException();
if (head == tail) {
head = tail = null;
return;
}
Node lastButOne = previous(tail);
lastButOne.next = null;
tail = lastButOne;
}
}
public class LinkedListFromScratch {
public static void main(String[] args) {
LinkedList myList = new LinkedList();
myList.addLast(2);
myList.addLast(4);
myList.addLast(8);
myList.addFirst(-2);
myList.addFirst(-4);
myList.addLast(9);
myList.print();
System.out.println(myList.indexOf(4));
System.out.println(myList.contains(9));
for (int i = 0; i < 6; ++i) {
myList.deleteLast();
myList.print();
}
}
}
In this linked list Add in an O(n) method that reverse to the class LinkedList created in the problem
1. This method must reverse the order of the nodes in the list. For
example, having applied reverse to the list [2 → 4 → 8 → 9 → 8], we
will change it to [8 → 9 → 8 → 4 → 2].
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)