Remove Nodes From Linked List

MediumLinked ListStackRecursionMonotonic Stack

Solution

import { ListNode } from '@algorithm/lib';
 
export function removeNodes(head: ListNode | null): ListNode | null {
  if (head === null) {
    return head;
  }
  head.next = removeNodes(head.next);
  return head.next && head.val < head.next.val ? head.next : head;
}