# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def nextLargerNodes(self, head):
        """
        :type head: ListNode
        :rtype: List[int]
        """
        h = head
        l = list()
        while(h): #ѭΪ˰ת
            l.append(h.val)
            h = h.next
            
        stack = list()
        res = [0 for i in range(len(l))]
        
        cnt = 0
        while(cnt < len(l)): #ǰɨ
            if not stack or l[stack[-1]] >= l[cnt]: #stackΪգߵǰջӦԪرȵǰɨԪػ
                stack.append(cnt)#ֱӰѵǰ±ѹջ
            else:#ǰ±ӦԪرջԪش󣬾˵ҵ˱ջԪشһ
                while(stack and l[stack[-1]] < l[cnt]): #һֱջֱջԪرȵǰԪС
                    res[stack[-1]] = l[cnt]                    
                    stack.pop()
                stack.append(cnt) #ѵǰԪѹջ
                
            cnt += 1
            
        return res
        