Remove nodes from linked list

December 11, 2022

linked-list

Problem URL: Remove nodes from linked list

We will use recursion to remove the nodes from the linked list. If the current node is not a leaf node, we will check if the next node is a leaf node. If it is, we will remove the next node. If it is not, we will call the function recursively on the next node. If the current node is a leaf node, we will return the current node.

# 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]:
        if not head:
            return head

        head.next = self.removeNodes(head.next)
        if head.next and head.val < head.next.val:
            return head.next

        return head

Time complexity: O(n)
Space complexity: O(n)