Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 29 ms, beats 84.99 % of java submissions
- class Solution {
- public int[] nextLargerNodes(ListNode head) {
- Map<ListNode, Integer> map = new HashMap<>();
- ListNode c = head;
- while (c != null) c = nextMax(c, map);
- int pos = 0;
- int[] result = new int[map.size()];
- c = head;
- while (c != null) {
- result[pos++] = map.get(c);
- c = c.next;
- }
- 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