aboutsummaryrefslogtreecommitdiff
path: root/remove_nodes_linked_list.py
blob: 75076c54bcfa83eca6e2d2f992272d453c2ec944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 2487. Remove Nodes From Linked List

"""
given the 'head' of a linked list, remove evey node which has a node with a
greater value anywhere to the right side of it. return the 'head' of the
modified linked list.
"""


# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution(object):
    def removeNodes(self, head):
        """
        :type head: Optional[ListNode]
        :rtype: Optional[ListNode]
        """
        if not head:
            return None
        head.next = self.removeNodes(head.next)
        if head.next and head.val < head.next.val:
            return head.next
        return head