# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
# add a extra head for removing head
prehead = ListNode(-1)
prehead.next = head
last, pos = prehead, head
while pos is not None:
if pos.val == val:
last.next = pos.next
else:
last = pos
pos = pos.next
return prehead.next
Download Remove Linked List Elements.pyLeetcode 203 Remove Linked List Elements problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.
Feedback is the most important part of any website.
If you have any query, suggestion or feedback, Please feel free to contact us.