List

class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
if head is None:return None
head.next = self.removeElements(head.next, val)
return head.next if head.val==val else head
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
dummy = ListNode(-1)
dummy.next = head
node = head
prev = dummy
while node is not None:
if node.val==val:
prev.next = node.next
node.next = None
node = prev.next
else:
prev = node
node = node.next
return dummy.next

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store