From 3389f6799cb39b16817ad22f49a7acfa36135dfe Mon Sep 17 00:00:00 2001 From: evanlai Date: Sun, 30 May 2021 14:07:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200203.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0=20python3=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0203.移除链表元素.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index dce5d265..cac9f233 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -208,7 +208,23 @@ public ListNode removeElements(ListNode head, int val) { ``` 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: ListNode, val: int) -> ListNode: + dummy_head = ListNode(next=head) #添加一个虚拟节点 + cur = dummy_head + while(cur.next!=None): + if(cur.next.val == val): + cur.next = cur.next.next #删除cur.next节点 + else: + cur = cur.next + return dummy_head.next +``` Go: