Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d56af71

Browse files
ybian19azl397985856
authored andcommitted
feat: 203.remove-linked-list-elements add Python3 implementation (azl397985856#124)
1 parent 12ac842 commit d56af71

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

‎problems/203.remove-linked-list-elements.md‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ if (!next || next.val !== val) {
3333

3434
```
3535

36-
3736
## 代码
3837

39-
```js
40-
38+
* 语言支持:JS,Python
4139

40+
Javascript Code:
4241

42+
```js
4343
/*
4444
* @lc app=leetcode id=203 lang=javascript
4545
*
@@ -54,14 +54,14 @@ if (!next || next.val !== val) {
5454
* Testcase Example: '[1,2,6,3,4,5,6]\n6'
5555
*
5656
* Remove all elements from a linked list of integers that have value val.
57-
*
57+
*
5858
* Example:
59-
*
60-
*
59+
*
60+
*
6161
* Input: 1->2->6->3->4->5->6, val = 6
6262
* Output: 1->2->3->4->5
63-
*
64-
*
63+
*
64+
*
6565
*/
6666
/**
6767
* Definition for singly-linked list.
@@ -95,6 +95,26 @@ var removeElements = function(head, val) {
9595

9696
return dummy.next;
9797
};
98-
9998
```
10099

100+
Python Code:
101+
102+
```python
103+
# Definition for singly-linked list.
104+
# class ListNode:
105+
# def __init__(self, x):
106+
# self.val = x
107+
# self.next = None
108+
109+
class Solution:
110+
def removeElements(self, head: ListNode, val: int) -> ListNode:
111+
prev = ListNode(0)
112+
prev.next = head
113+
cur = prev
114+
while cur.next:
115+
if cur.next.val == val:
116+
cur.next = cur.next.next
117+
else:
118+
cur = cur.next
119+
return prev.next
120+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /