Next Greater Node In Linked List LeetCode solution in C++

1
class Solution {
2
public:
3
vector<int> nextLargerNodes(ListNode *head) {
4
vector<int> ans;
5
ListNode *curr = head;
6
if (head->next == NULL) {
7
ans.push_back(0);
8
return ans;
9
}
10

11
while (curr != NULL) {
12
ListNode *next = curr->next;
13
while (next != NULL) {
14
if (next->val > curr->val) {
15
ans.push_back(next->val);
16
break;
17
}
18
next = next->next;
19
}
20
if (next == NULL) {
21
ans.push_back(0);
22
}
23
curr = curr->next;
24
}
25
return ans;
26
}
27
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0