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

Add solution at #203.移除鏈表元素 #2911

Open
wants to merge 2 commits 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
24 changes: 23 additions & 1 deletion problems/0203.移除链表元素.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down