Skip to content

Commit 3389f67

Browse files
committed
添加 0203.移除链表元素 python3版本
1 parent 34f420c commit 3389f67

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

problems/0203.移除链表元素.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,23 @@ public ListNode removeElements(ListNode head, int val) {
208208
```
209209

210210
Python:
211-
211+
```python
212+
# Definition for singly-linked list.
213+
# class ListNode:
214+
# def __init__(self, val=0, next=None):
215+
# self.val = val
216+
# self.next = next
217+
class Solution:
218+
def removeElements(self, head: ListNode, val: int) -> ListNode:
219+
dummy_head = ListNode(next=head) #添加一个虚拟节点
220+
cur = dummy_head
221+
while(cur.next!=None):
222+
if(cur.next.val == val):
223+
cur.next = cur.next.next #删除cur.next节点
224+
else:
225+
cur = cur.next
226+
return dummy_head.next
227+
```
212228

213229
Go:
214230

0 commit comments

Comments
 (0)