Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 0203.移除链表元素.md(补充Python直接在原链表操作的代码) #2905

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions problems/0203.移除链表元素.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down