/ Published in: Java
Reverse a linked list using Java.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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>(); list.add("one"); list.add("two"); list.add("three"); list.add("four"); reverseList( list ); printList( reversedList ); } public static void reverseList( List<String> listarg ) { 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() ) { } } }