Created
May 15, 2020 01:48
-
-
Save abrarShariar/bb7b7d6e5b295bc233ff0885c945b575 to your computer and use it in GitHub Desktop.
code for finding the next greatest node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: ListNode) -> List[int]: | |
if head == None: | |
return | |
input_list = [] | |
while head: | |
input_list.append(head.val) | |
head = head.next | |
stack = [] | |
n = len(input_list) | |
output_list = [0 for i in range(n)] | |
for i in range(n): | |
while len(stack) and input_list[stack[-1]] < input_list[i]: | |
j = stack.pop() | |
output_list[j] = input_list[i] | |
stack.append(i) | |
return output_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment