Return to Snippet

Revision: 61445
at December 13, 2012 11:30 by djangofan


Initial Code
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Recur {
	
  private static List<String> list = new ArrayList<String>();
  private static List<String> reversedList = new ArrayList<String>();

  public static void main( String[] args ) {
    list.add("one");
    list.add("two");
    list.add("three");
    list.add("four");
		
    reverseList( list );

    printList( reversedList );
  }

  public static void reverseList( List<String> listarg ) {
    String firstitem = null;
    if ( listarg.size() == 0 ) {
      return;
    } else {
      firstitem = listarg.get(0);
      listarg.remove(0);
    }
    reverseList( listarg );
    reversedList.add( firstitem );
  } 
	
  public static void printList( List<String> listarg ) {
    Iterator<String> listIterator = listarg.iterator();
    while( listIterator.hasNext() )  {
      String item = listIterator.next();
      System.out.println( item );
    }
  }
}

Initial URL

                                

Initial Description
Reverse a linked list using Java.

Initial Title
Java - reverse a linked list

Initial Tags
java

Initial Language
Java