Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 41 ms, runtime beats 53.07 % of java submissions.
- class Solution {
- public int[] nextLargerNodes(ListNode head) {
- ArrayList<Integer> r = new ArrayList<>();
- Map<ListNode, Integer> map = new HashMap<>();
- ListNode c = head;
- while (c != null) {
- if (!map.containsKey(c)) nextMax(c, map);
- r.add(map.get(c));
- c = c.next;
- }
- int[] result = new int[r.size()];
- for (int i = 0; i < result.length; i++) result[i] = r.get(i);
- return result;
- }
- private ListNode nextMax(ListNode from, Map<ListNode,Integer> map) {
- ListNode next = from.next;
- while (next != null) {
- if (next.val > from.val){
- map.put(from, next.val);
- return next;
- }
- next = nextMax(next, map);
- }
- map.put(from, 0);
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement