Skip to content

Commit 8aec589

Browse files
authored
Create next-greater-node-in-linked-list.py
1 parent 95d6b4d commit 8aec589

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
# Definition for singly-linked list.
5+
class ListNode(object):
6+
def __init__(self, x):
7+
self.val = x
8+
self.next = None
9+
10+
11+
class Solution(object):
12+
def nextLargerNodes(self, head):
13+
"""
14+
:type head: ListNode
15+
:rtype: List[int]
16+
"""
17+
result, stk = [], []
18+
while head:
19+
while stk and stk[-1][1] < head.val:
20+
result[stk.pop()[0]] = head.val
21+
stk.append([len(result), head.val])
22+
result.append(0)
23+
head = head.next
24+
return result

0 commit comments

Comments
 (0)