Last active
March 13, 2021 04:37
-
-
Save liyunrui/940ed49b7a925c32e454c5c0e874211f to your computer and use it in GitHub Desktop.
leetcode-ll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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