diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index 5a4bbb7423..11134f13e1 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -369,9 +369,33 @@ class Solution { ``` ### Python: - +直接使用原链表 +```python +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]: + while head != None and head.val == val: + head = head.next + if head == None: + return None + + cur = head.next + pre = head + while cur != None: + if cur.val == val: + pre.next = cur.next + else: + pre = pre.next + cur = cur.next + + return head +``` +虚拟头节点法 ```python -(版本一)虚拟头节点法 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None):