Question: I am having difficulty getting my reverse method to work properly. The method needs to reverse the order of nodes in a linked list. Please

I am having difficulty getting my "reverse" method to work properly. The method needs to reverse the order of nodes in a linked list. Please see my code below, correct it and advise me what I did wrong. Thanks!

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 void reverse() {

Node current = head;

Node previous = null;

Node next = null;

while (current != null) {

next = current.next;

current.next = previous;

previous = current;

current = next;

}

}

}

public class LinkedListFromScratch {

public static void main(String[] args) {

LinkedList list = new LinkedList();

list.addLast(2);

list.addLast(4);

list.addLast(8);

list.addFirst(-2);

list.addFirst(-4);

list.addLast(9);

list.print();

list.reverse();

list.print();

System.out.println(list.indexOf(4));

System.out.println(list.contains(9));

for (int i = 0; i < 6; ++i) {

list.deleteLast();

list.print();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!