// Insert at tail of list - the recursive version... import java.io.*; class ListElement { int value; ListElement next; } class Linklist2r { static ListElement Insert(ListElement head, ListElement newelem) { ListElement help; if (head == null) { // Empty list... // I know exactly where the newelem element will go. newelem.next = null; return(newelem); } else { // I know the "newelem" element goes after "head", but // I don't have a clue where the "newelem" element goes... // Recurse and get help.... help = Insert(head.next, newelem); // OK, the recursed version of myself made the insertion.. // Complete the list and return.... head.next = help; return(head); } } public static void Print(ListElement head) { while (head != null) { System.out.print(head.value + " "); head = head.next; } System.out.println(); } public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); ListElement head, ptr; head = null; while(true) { ptr = new ListElement(); System.out.print("Enter number: "); ptr.value = Integer.parseInt(stdin.readLine()); head = Insert(head, ptr); System.out.print("List: "); Print(head); System.out.println(); } } }