From a147b15f760ed701ddb9e99c7865458bd643641e Mon Sep 17 00:00:00 2001 From: LuREkaWd <106424188+LuREkaWd@users.noreply.github.com> Date: Thu, 6 Mar 2025 17:47:32 +0800 Subject: [PATCH 1/2] =?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 add another version solution you mention above --- ...76\350\241\250\345\205\203\347\264\240.md" | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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..9b5ad08935 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,31 @@ 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 is not None and head.val == val: + head = head.next + + cur = head + # 刪除非頭節點 + while head is not None and cur.next: + if cur.next.val == val: + cur.next = cur.next.next + else: + cur = cur.next + return head +``` +設置虛擬頭節點: ```python -(版本一)虚拟头节点法 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): From 58c7b406be1fa96ad72cbf3cf62c9dc69c574ccd Mon Sep 17 00:00:00 2001 From: LuREkaWd <106424188+LuREkaWd@users.noreply.github.com> Date: Thu, 6 Mar 2025 17:48:43 +0800 Subject: [PATCH 2/2] =?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 change some typo --- ...\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 9b5ad08935..65f7681549 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,7 +369,7 @@ class Solution { ``` ### Python: -使用原練表操作: +使用原鏈表操作: ```python # Definition for singly-linked list. # class ListNode: