static Node Reverse ( Node head , int k ) {
  Stack < Node > mystack = new Stack < Node > ( ) ;
  Node current = head ;
  Node prev = null ;
  while ( current != null ) {
    int count = 0 ;
    while ( current != null && count < k ) {
      mystack . push ( current ) ;
      current = current . next ;
      count ++ ;
    }
    while ( mystack . size ( ) > 0 ) {
      if ( prev == null ) {
        prev = mystack . peek ( ) ;
        head = prev ;
        mystack . pop ( ) ;
      }
      else {
        prev . next = mystack . peek ( ) ;
        prev = prev . next ;
        mystack . pop ( ) ;
      }
    }
  }
  prev . next = null ;
  return head ;
}

static Node Reverse ( Node head , int k ) { Stack < Node > mystack = new Stack < Node > ( ) ; Node current = head ; Node prev = null ; while ( current != null ) { int count = 0 ; while ( current != null && count < k ) { mystack . push ( current ) ; current = current . next ; count ++ ; } while ( mystack . size ( ) > 0 ) { if ( prev == null ) { prev = mystack . peek ( ) ; head = prev ; mystack . pop ( ) ; } else { prev . next = mystack . peek ( ) ; prev = prev . next ; mystack . pop ( ) ; } } } prev . next = null ; return head ; }
(method_declaration (modifiers) type: (type_identifier) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments (type_identifier))) arguments: (argument_list)))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (identifier))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (null_literal))) (while_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (null_literal))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (null_literal)) right: (binary_expression left: (identifier) right: (identifier)))) body: (block (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) (expression_statement (assignment_expression left: (identifier) right: (field_access object: (identifier) field: (identifier)))) (expression_statement (update_expression (identifier))))) (while_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (null_literal))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (expression_statement (assignment_expression left: (identifier) right: (identifier))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) alternative: (block (expression_statement (assignment_expression left: (field_access object: (identifier) field: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (expression_statement (assignment_expression left: (identifier) right: (field_access object: (identifier) field: (identifier)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))))))))) (expression_statement (assignment_expression left: (field_access object: (identifier) field: (identifier)) right: (null_literal))) (return_statement (identifier))))