solution to reverseKGroup