Java code for leetcode