1+ # Definition for singly-linked list.
2+ # class ListNode:
3+ # def __init__(self, val=0, next=None):
4+ # self.val = val
5+ # self.next = next
6+
7+ class Solution :
8+ def nextLargerNodes (self , head : Optional [ListNode ]) -> List [int ]:
9+
10+ # ----------------------------------------------------------
11+ # Step 1: Convert Linked List to Array
12+ # Example:
13+ # Linked List : 2 -> 7 -> 4 -> 3 -> 5
14+ # Array : [2, 7, 4, 3, 5]
15+ # ----------------------------------------------------------
16+ nums = []
17+ curr = head
18+
19+ while curr :
20+ nums .append (curr .val )
21+ curr = curr .next
22+
23+ # ==========================================================
24+ # APPROACH 1 : BRUTE FORCE
25+ # ----------------------------------------------------------
26+ # How it works:
27+ # For every element, search all elements on its right.
28+ # The first greater element becomes the answer.
29+ #
30+ # Example:
31+ # nums = [2,7,4,3,5]
32+ #
33+ # 2 -> first greater = 7
34+ # 7 -> no greater = 0
35+ # 4 -> first greater = 5
36+ # 3 -> first greater = 5
37+ # 5 -> no greater = 0
38+ #
39+ # Time Complexity : O(n²)
40+ # Space Complexity: O(n)
41+ # ==========================================================
42+
43+ """
44+ ans = [0] * len(nums)
45+
46+ i = 0
47+ while i < len(nums):
48+
49+ j = i + 1
50+
51+ while j < len(nums):
52+
53+ if nums[j] > nums[i]:
54+ ans[i] = nums[j]
55+ break
56+
57+ j += 1
58+
59+ i += 1
60+
61+ return ans
62+ """
63+
64+ # ==========================================================
65+ # APPROACH 2 : MONOTONIC STACK (OPTIMAL)
66+ #
67+ # How it works:
68+ #
69+ # The stack stores INDICES of elements whose next greater
70+ # element has NOT been found yet.
71+ #
72+ # Whenever a larger value arrives:
73+ #
74+ # 1. Compare it with the top of the stack.
75+ # 2. If current value is larger,
76+ # pop the index.
77+ # 3. Current value becomes the answer
78+ # for the popped index.
79+ # 4. Keep popping while current value
80+ # is larger than the stack top.
81+ # 5. Finally push the current index.
82+ #
83+ # Example:
84+ #
85+ # nums = [2,7,4,3,5]
86+ #
87+ # i=0 (2)
88+ # stack = [0]
89+ #
90+ # i=1 (7)
91+ # 7 > 2
92+ # pop 0
93+ # ans[0]=7
94+ # stack=[1]
95+ #
96+ # i=2 (4)
97+ # 4 < 7
98+ # stack=[1,2]
99+ #
100+ # i=3 (3)
101+ # 3 < 4
102+ # stack=[1,2,3]
103+ #
104+ # i=4 (5)
105+ # 5 > 3
106+ # pop 3
107+ # ans[3]=5
108+ #
109+ # 5 > 4
110+ # pop 2
111+ # ans[2]=5
112+ #
113+ # 5 < 7
114+ # stop
115+ #
116+ # push 4
117+ # stack=[1,4]
118+ #
119+ # Final Answer:
120+ # [7,0,5,5,0]
121+ #
122+ # Time Complexity : O(n)
123+ # Space Complexity: O(n)
124+ #
125+ # Why O(n)?
126+ # - Every index is pushed exactly once.
127+ # - Every index is popped at most once.
128+ # - Total stack operations = 2n.
129+ # - Therefore overall complexity is O(n).
130+ # ==========================================================
131+
132+ ans = [0 ] * len (nums )
133+ stack = [] # Stores indices
134+
135+ for i in range (len (nums )):
136+
137+ # Current element is greater than elements
138+ # waiting inside the stack.
139+ while stack and nums [i ] > nums [stack [- 1 ]]:
140+ idx = stack .pop ()
141+ ans [idx ] = nums [i ]
142+
143+ # Current index waits for its next greater element.
144+ stack .append (i )
145+
146+ return ans
0 commit comments