Java code for Leetcode