SHARE
TWEET

Untitled

a guest Apr 18th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. #     def __init__(self, x):
  4. #         self.val = x
  5. #         self.next = None
  6.  
  7. class Solution:
  8.     def nextLargerNodes(self, head: ListNode) -> List[int]:
  9.         vals = []
  10.         val = head
  11.         while val != None:
  12.             vals.append(val.val)
  13.             val = val.next
  14.        
  15.         stack = []
  16.         larger = []
  17.         for i in range(len(vals)-1, -1, -1):
  18.             # print(stack)
  19.             while stack and vals[i] >= stack[-1]:
  20.                 stack = stack[:-1]
  21.             if not stack:
  22.                 larger.append(0)
  23.             else:
  24.                 larger.append(stack[-1])
  25.             stack.append(vals[i])
  26.         larger.reverse()
  27.         return larger
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top