Skip to content

Latest commit

 

History

History
57 lines (44 loc) · 1.45 KB

_1019. Next Greater Node In Linked List.md

File metadata and controls

57 lines (44 loc) · 1.45 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 22, 2024

Last updated : June 22, 2024


Related Topics : Array, Linked List, Stack, Monotonic Stack

Acceptance Rate : 61.33 %


Solutions

Python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]:
        curr = head
        output = []
        size = 0
        while curr :
            output.append(curr.val)
            curr = curr.next
            size += 1

        largest = []
        for i in range(size - 1, -1, -1) :
            while largest and largest[-1] <= output[i] :
                largest.pop()
            
            if largest :
                largest.append(output[i])
                output[i] = largest[-2]
            else :
                largest.append(output[i])
                output[i] = 0
                continue

        return output