Simplify the code in getNextGreaterElement