here is iteratively class Solution: def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]: # reverse the list pre, cur = None, head while cur: temp = cur.next cur.next = pre pre = cur cur = temp # remove the node that is less than the max(greater value to the right) head = pre cur = pre.next max = pre.val while cur: if cur.val < max: pre.next = cur.next cur.next = None cur = pre.next else: max = cur.val pre = cur cur = cur.next # reverse the list again pre, cur = None, head while cur: temp = cur.next cur.next = pre pre = cur cur = temp return pre
loved ur explanation on this problem, the way u explained the hashmap to us, tht helped alot in understanding the working of the code, none in the youtube community does tht and it gets super confusing, keep up the good work, Thank You!!!!!!!!!
For iteratively You have to first reverse the list . Then traverse it from end ( it means from new head ) then take a Max value and check if a number is greater then current value the update max and if you get smaller value then you have to make connection between current node to those node which have greater value then finally reverse again the list and return head of list . I hope it helps you .