-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnext_greater_node_in_linked_list.py
More file actions
44 lines (31 loc) · 996 Bytes
/
next_greater_node_in_linked_list.py
File metadata and controls
44 lines (31 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 1019. Next Greater Node In Linked List
# https://leetcode.com/problems/next-greater-node-in-linked-list/
from typing import List
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def nextLargerNodes(self, head: ListNode) -> List[int]:
nums = []
while head is not None:
nums.append(head.val)
head = head.next
ans = [0 for _ in range(len(nums))]
stack = []
idx = 0
while idx < len(nums):
while len(stack) > 0 and nums[idx] > stack[-1][1]:
ans[stack.pop()[0]] = nums[idx]
stack.append((idx, nums[idx]))
idx += 1
print(stack, ans)
return ans
if __name__ == '__main__':
node = ListNode(1)
node.next = ListNode(1)
node.next.next = ListNode(1)
node.next.next.next = ListNode(3)
sol = Solution()
print(sol.nextLargerNodes(node))