classSolution{publicListNoderemoveNodes(ListNode head){// Reverse the list
head =reverse(head);ListNode cur = head;int max = cur.val;// Iterate in reversed orderwhile(cur.next !=null){// Update max value
max =Math.max(max, cur.val);if(max > cur.next.val){// Skip the next node
cur.next = cur.next.next;}else{// Actually move the pointer
cur = cur.next;}}// Reverse againreturnreverse(head);}privateListNodereverse(ListNode head){ListNode prev =null;// Reverse the listwhile(head !=null){ListNode next = head.next;
head.next = prev;
prev = head;
head = next;}return prev;}}