Advertisement
ogv

Untitled

ogv
Sep 30th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. // Runtime: 29 ms, beats 84.99 % of java submissions
  2. class Solution {
  3.     public int[] nextLargerNodes(ListNode head) {
  4.         Map<ListNode, Integer> map = new HashMap<>();
  5.        
  6.         ListNode c = head;
  7.         while (c != null) c = nextMax(c, map);
  8.        
  9.         int pos = 0;
  10.         int[] result = new int[map.size()];
  11.        
  12.         c = head;
  13.         while (c != null) {
  14.             result[pos++] = map.get(c);
  15.             c = c.next;
  16.         }
  17.        
  18.         return result;
  19.     }
  20.    
  21.     private ListNode nextMax(ListNode from, Map<ListNode,Integer> map) {
  22.         ListNode next = from.next;
  23.         while (next != null) {
  24.             if (next.val > from.val){
  25.                 map.put(from, next.val);
  26.                 return next;
  27.             }
  28.             next = nextMax(next, map);
  29.         }
  30.         map.put(from, 0);
  31.         return null;
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement