class List { int value; List next; public List(int x) { value = x; } } public class Insert { public static List insert( List head, List e ) { List helpSol; /* -------------------------------------------- Base case: insert at the tail of an empty -------------------------------------------- */ if ( head == null ) { e.next = null; // Mark e as the last list elem return(e); // e is the first list elem ! } else { /* =========================================================== Solve the problem USING the solution of a smaller problem =========================================================== */ helpSol = insert( head.next, e ); // Have "someone else" solve // this smaller problem head.next = helpSol; // Find MY solution using helpSol return head; // Return MY solution } } public static void print(List h) { while ( h != null ) { System.out.print(h.value + " "); h = h.next; } } public static void main(String[] args) { int[] inp = {1, 2, 3, 4, 5, 6, 7}; List head = null; for ( int i = 0; i < inp.length; i++ ) { System.out.print("List before inserting: " + inp[i] + ": "); print(head); System.out.println(); head = insert(head, new List(inp[i]) ); System.out.print("List after inserting: " + inp[i] + ": "); print(head); System.out.println("\n"); } } }