通过万岁!!!
class Solution {
public ListNode removeNodes(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode ret = head;
Stack<ListNode> stack = new Stack<>();
while (head != null) {
if (stack.isEmpty()) {
stack.push(head);
ret = head;
} else {
while (!stack.isEmpty() && stack.peek().val < head.val) {
stack.pop();
}
if (!stack.isEmpty()) {
stack.peek().next = head;
} else {
ret = head;
}
stack.push(head);
}
head = head.next;
}
return ret;
}
}