Skip to content

Instantly share code, notes, and snippets.

@liyunrui
Last active March 13, 2021 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liyunrui/940ed49b7a925c32e454c5c0e874211f to your computer and use it in GitHub Desktop.
Save liyunrui/940ed49b7a925c32e454c5c0e874211f to your computer and use it in GitHub Desktop.
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