Next Greater Node In Linked List LeetCode solution in Python

1
class Solution:
2
def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]:
3
stack = []
4
ans = []
5
node = head
6

7
i = 0
8
while node is not None:
9
while stack and stack[-1][0] < node.val:
10
ans[stack[-1][1]] = node.val
11
stack.pop()
12

13
stack.append((node.val, i))
14
ans.append(0)
15
i += 1
16
node = node.next
17

18
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0