Let's make this one less mysterious. For 1019. Next Greater Node In Linked List, the solution in this repository is mainly a stack solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: stack.
The notes already sitting in the source point us in the right direction:
- Hint 1: We can use a stack that stores nodes in monotone decreasing order of value. When we see a node_j with a larger value, every node_i in the stack has next_larger(node_i) = node_j .
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are nextLargerNodes.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Hint 1: We can use a stack that stores nodes in monotone decreasing order of value. When we see a node_j with a larger value, every node_i in the stack has next_larger(node_i) = node_j .
02//Runtime: 208 ms, faster than 84.96% of C++ online submissions for Next Greater Node In Linked List.
03//Memory Usage: 25.5 MB, less than 58.82% of C++ online submissions for Next Greater Node In Linked List.
04/**
05 * Definition for singly-linked list.
06 * struct ListNode {
07 * int val;
08 * ListNode *next;
09 * ListNode(int x) : val(x), next(NULL) {}
10 * };
11 */
12class Solution {
13public:
14 vector<int> nextLargerNodes(ListNode* head) {
15 ListNode* cur = head;
16 vector<int> inputs;
17 while(cur){
18 inputs.push_back(cur->val);
19 cur = cur->next;
20 }
21
22 vector<int> ans(inputs.size(), 0);
23 stack<int> stk;
24
25 for(int i = 0; i < inputs.size(); i++){
26 //stk's elements are in decreasing order
27 //pop all elements <= inputs[i] and set their corresponding ans
28 //the other elements remains in the stack
29 while(!stk.empty() && inputs[i] > inputs[stk.top()]){
30 ans[stk.top()] = inputs[i];
31 stk.pop();
32 }
33 stk.push(i);
34 }
35
36 return ans;
37 }
38};
Cost