X784: Partial Reverse

Consider the following class definitions:

public class LinkedChain<T> {
    private Node<T> firstNode;
    private int numberOfEntries;

    public LinkedChain() {
        firstNode = null;
        numberOfEntries = 0;
    }// end default constructor

    ...
}

Where Node is defined as:

public class Node<T> {
     private T data; // Entry in bag
     private Node<T> next; // Link to next node

     public Node(T dataPortion) {
         this(dataPortion, null);
     } // end constructor


     public Node(T dataPortion, Node<T> nextNode) {
         data = dataPortion;
         next = nextNode;
     } // end constructor


     public T getData() {
         if (data != null) {
             return data;
         }
         return null;
     }


     public Node getNext() {
         return next;
     }


     public void setNext(Node<T> newNext) {
         next = newNext;
     }
}

Below, write a Linked Chain method that will take in a reference to a node and reverse the list up to and including that point.

For example, if the linked chain looked like this:

A --> B --> C --> D --> E --> F

and the parameter was:

desiredNode = the node containing D

this method would change the chain to:

D --> C --> B --> A --> E --> F

If the node referenced is null or not in the linked chain, the code should not alter the linked chain

Your Answer:

Reset
Visualize

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.