Java Code | Easy to Understand | For Beginners | Beats 100% Runtime 1ms
Approach\n- Initialize k as 0.\n- On getting non -1 numbers, append it to the front of seen list.\n- If you get a -1 for the first time, update k to 1 and add t
# Approach\n- Initialize k as 0.\n- On getting non -1 numbers, append it to the front of seen list.\n- If you get a -1 for the first time, update k to 1 and add the 1st element of seen list to ans list, if seen list is empty, add -1\n- Post the first occurance, if there are more consecutive -1\'s increase the value of k and take the kth from seen list, or add -1 if size of k is >= seen list\n- If after -1 you get a non -1 number, add it to 0th position of seen list and reset value of k to 0\n\n# Code\n```java []\nclass Solution {\n    public List<Integer> lastVisitedIntegers(int[] nums) {\n        List<Integer> ans = new ArrayList<>();\n        List<Integer> seen = new ArrayList<>();\n        int k = 0;\n        for(int i=0;i<nums.length;i++) {\n            if(nums[i]>-1) {\n                seen.add(0,nums[i]);\n                k = 0;\n            }\n            else {\n                ans.add(k>=seen.size() ? -1 : seen.get(k));\n                k++;\n            }\n        }\n        return ans; \n    }\n}\n```
['Java']