From 1c801b994485a4f015b0fa426452ce3d22cf79c4 Mon Sep 17 00:00:00 2001 From: SupppRabit <55012063+SupppRabit@users.noreply.github.com> Date: Fri, 28 Feb 2025 17:54:40 +0800 Subject: [PATCH] =?UTF-8?q?Update=200203.=E7=A7=BB=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\241\250\345\205\203\347\264\240.md" | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) 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):