class List( int value; List next; ); void printList( List source; ) { print( "{ " ); while ( source != null ) { print( source.value ); source = source.next; if ( source != null ) print( ", " ); } println( " }" ); } void createNode( int level; var List dest; int value; List next; ) { dest = new List{ value, next }; // Show state at this point } void insertList( int level; List source; var List dest; int value; ) { if ( source == null || value <= source.value ) { createNode( level + 1, dest, value, source ); // Inside the above invocation } else { createNode( level + 1, dest, source.value, null ); insertList( level + 1, source.next, dest.next, value ); // Inside the above invocation } } List source, a2, a4, a7, a9, dest; a9 = new List{ 9, null }; a7 = new List{ 7, a9 }; a4 = new List{ 4, a7 }; a2 = new List{ 2, a4 }; source = a2; insertList( 0, source, dest, 5 ); // Inside this invocation printList( source ); printList( dest );