Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
leetcode-ll
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
"""
d->7->-7>-7->7
perv cur
"""
def removeElements(self, head: ListNode, val: int) -> ListNode:
if not head:
return head
d = ListNode(-1)
d.next = head
prev = d
cur = d.next
while cur:
if cur.val == val:
prev.next = cur.next
else:
prev = prev.next
cur = cur.next
return d.next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment