Hi, I'm writing a program that reverses the lines of a String using the LinkedList structure, which includes an iterator. I have my code written out, but every time my program runs, it takes a very long time before it outputs the lines. I'm having difficulty proceeding to the next step. If anyone could point me to the right direction on what to do next, I would greatly appreciate it.
Also note: I'm unable to call the reverse() method from the Collections class.
import java.util.*; import java.util.ListIterator; import java.io.*; public class ReverseList { public static void main (String[] args) { LinkedList<String> phrase = new LinkedList<String>(); phrase.add("Four"); phrase.add("score"); phrase.add("and"); phrase.add("seven"); phrase.add("years"); phrase.add("ago"); System.out.println("Phrase in normal order: " + phrase); reverse(phrase); System.out.println("Phrase in reverse order: " + phrase); } public static LinkedList<String> reverse(LinkedList<String> a) { ListIterator<String> order = a.listIterator(a.size()); LinkedList<String> reversed = new LinkedList<String>(); while(order.hasPrevious()) { reversed.add(order.previous()); } return reversed; } }