Skip to content

Latest commit

 

History

History
54 lines (39 loc) · 1.43 KB

_2487. Remove Nodes From Linked List.md

File metadata and controls

54 lines (39 loc) · 1.43 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 12, 2024

Last updated : July 01, 2024


Related Topics : Linked List, Stack, Recursion, Monotonic Stack

Acceptance Rate : 74.42 %


Solutions

Python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode()
        dummy.next = head
        dummy.val = inf

        def helper(currentNode: Optional[ListNode]) -> Optional[ListNode()]:
            if not currentNode :
                return None
            currentNode.next = helper(currentNode.next)

            if not currentNode.next :
                return currentNode
            
            if currentNode.next.val > currentNode.val :
                return currentNode.next
            return currentNode
        
        dummy = helper(dummy)

        return dummy.next