解题思路 逆置链表 通过栈来进行操作 不需要额外开辟动态链表
class Solution
{
int len=0;
ListNode* reverseList(ListNode* head)
{
ListNode*dummy=nullptr;
ListNode*curr=head;
while(curr!=nullptr)
{
ListNode*next=curr->next;
curr->next=dummy;
dummy=curr;
curr=next;
len++;
}
return dummy;
}
public:
vector<int> nextLargerNodes(ListNode* head)
{
if(head==nullptr)return {};
if(head->next==nullptr)return {0};
ListNode *p=reverseList(head);
vector<int> ans(len);
stack<int> s;
while(p!=nullptr)
{
while(!s.empty()&&s.top()<=p->val)s.pop();
len--;
ans[len]=s.empty()?0:s.top();
s.push(p->val);
p=p->next;
}
return ans;
}
};