Skip to content

Instantly share code, notes, and snippets.

@abrarShariar
Created May 15, 2020 01:48
Show Gist options
  • Save abrarShariar/bb7b7d6e5b295bc233ff0885c945b575 to your computer and use it in GitHub Desktop.
Save abrarShariar/bb7b7d6e5b295bc233ff0885c945b575 to your computer and use it in GitHub Desktop.
code for finding the next greatest node
# 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