static void extend ( LinkedList < Integer > ans , List < ArrayList < Integer >> connect , boolean [ ] taken , boolean right ) {
  while ( true ) {
    Integer tmp = right ? ans . getLast ( ) : ans . getFirst ( ) ;
    boolean found = false ;
    for ( Integer i : connect . get ( tmp ) ) {
      if ( ! taken [ i ] ) {
        taken [ i ] = true ;
        if ( right ) {
          ans . addLast ( i ) ;
        }
        else {
          ans . addFirst ( i ) ;
        }
        found = true ;
        break ;
      }
    }
    if ( ! found ) {
      break ;
    }
  }
}

static void extend ( LinkedList < Integer > ans , List < ArrayList < Integer > > connect , boolean [ ] taken , boolean right ) { while ( true ) { Integer tmp = right ? ans . getLast ( ) : ans . getFirst ( ) ; boolean found = false ; for ( Integer i : connect . get ( tmp ) ) { if ( ! taken [ i ] ) { taken [ i ] = true ; if ( right ) { ans . addLast ( i ) ; } else { ans . addFirst ( i ) ; } found = true ; break ; } } if ( ! found ) { break ; } } }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (generic_type (type_identifier) (type_arguments (type_identifier))) name: (identifier)) (formal_parameter type: (generic_type (type_identifier) (type_arguments (generic_type (type_identifier) (type_arguments (type_identifier))))) name: (identifier)) (formal_parameter type: (array_type element: (boolean_type) dimensions: (dimensions)) name: (identifier)) (formal_parameter type: (boolean_type) name: (identifier))) body: (block (while_statement condition: (parenthesized_expression (true)) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (ternary_expression condition: (identifier) consequence: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) alternative: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))))) (local_variable_declaration type: (boolean_type) declarator: (variable_declarator name: (identifier) value: (false))) (enhanced_for_statement type: (type_identifier) name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) body: (block (if_statement condition: (parenthesized_expression (unary_expression operand: (array_access array: (identifier) index: (identifier)))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (true))) (if_statement condition: (parenthesized_expression (identifier)) consequence: (block (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))) alternative: (block (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))))) (expression_statement (assignment_expression left: (identifier) right: (true))) (break_statement))))) (if_statement condition: (parenthesized_expression (unary_expression operand: (identifier))) consequence: (block (break_statement)))))))