aboutsummaryrefslogtreecommitdiff
path: root/remove_nodes_linked_list.py
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /remove_nodes_linked_list.py
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'remove_nodes_linked_list.py')
-rw-r--r--remove_nodes_linked_list.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/remove_nodes_linked_list.py b/remove_nodes_linked_list.py
new file mode 100644
index 0000000..75076c5
--- /dev/null
+++ b/remove_nodes_linked_list.py
@@ -0,0 +1,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