Skip to content

Commit c251452

Browse files
committedFeb 5, 2021
feat: add python and java solutions to leetcode problem: No.0203
1 parent 65cb699 commit c251452

File tree

6 files changed

+96
-27
lines changed

6 files changed

+96
-27
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272

7373
- [从尾到头打印链表](/lcof/面试题06.%20从尾到头打印链表/README.md)
7474
- [删除链表的节点](/lcof/面试题18.%20删除链表的节点/README.md)
75+
- [移除链表元素](/solution/0200-0299/0203.Remove%20Linked%20List%20Elements/README.md)
7576
- [链表中倒数第 k 个节点](/lcci/02.02.Kth%20Node%20From%20End%20of%20List/README.md)
7677
- [两两交换链表中的节点](/solution/0000-0099/0024.Swap%20Nodes%20in%20Pairs/README.md)
7778
- [合并两个有序链表](/solution/0000-0099/0021.Merge%20Two%20Sorted%20Lists/README.md)

‎README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
7070
### Linked List
7171

7272
- [Delete Node in a Linked List](/solution/0200-0299/0237.Delete%20Node%20in%20a%20Linked%20List/README_EN.md)
73+
- [Remove Linked List Elements](/solution/0200-0299/0203.Remove%20Linked%20List%20Elements/README_EN.md)
7374
- [Kth Node From End of List](/lcci/02.02.Kth%20Node%20From%20End%20of%20List/README_EN.md)
7475
- [Swap Nodes in Pairs](/solution/0000-0099/0024.Swap%20Nodes%20in%20Pairs/README_EN.md)
7576
- [Merge Two Sorted Lists](/solution/0000-0099/0021.Merge%20Two%20Sorted%20Lists/README_EN.md)

‎solution/0200-0299/0203.Remove Linked List Elements/README.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,49 @@
2424
<!-- 这里可写当前语言的特殊实现逻辑 -->
2525

2626
```python
27-
27+
# Definition for singly-linked list.
28+
# class ListNode:
29+
# def __init__(self, val=0, next=None):
30+
# self.val = val
31+
# self.next = next
32+
class Solution:
33+
def removeElements(self, head: ListNode, val: int) -> ListNode:
34+
dummy = ListNode(-1, head)
35+
pre = dummy
36+
while pre and pre.next:
37+
if pre.next.val != val:
38+
pre = pre.next
39+
else:
40+
pre.next = pre.next.next
41+
return dummy.next
2842
```
2943

3044
### **Java**
3145

3246
<!-- 这里可写当前语言的特殊实现逻辑 -->
3347

3448
```java
35-
49+
/**
50+
* Definition for singly-linked list.
51+
* public class ListNode {
52+
* int val;
53+
* ListNode next;
54+
* ListNode() {}
55+
* ListNode(int val) { this.val = val; }
56+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
57+
* }
58+
*/
59+
class Solution {
60+
public ListNode removeElements(ListNode head, int val) {
61+
ListNode dummy = new ListNode(-1, head);
62+
ListNode pre = dummy;
63+
while (pre != null && pre.next != null) {
64+
if (pre.next.val != val) pre = pre.next;
65+
else pre.next = pre.next.next;
66+
}
67+
return dummy.next;
68+
}
69+
}
3670
```
3771

3872
### **...**

‎solution/0200-0299/0203.Remove Linked List Elements/README_EN.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,47 @@
2323
### **Python3**
2424

2525
```python
26-
26+
# Definition for singly-linked list.
27+
# class ListNode:
28+
# def __init__(self, val=0, next=None):
29+
# self.val = val
30+
# self.next = next
31+
class Solution:
32+
def removeElements(self, head: ListNode, val: int) -> ListNode:
33+
dummy = ListNode(-1, head)
34+
pre = dummy
35+
while pre and pre.next:
36+
if pre.next.val != val:
37+
pre = pre.next
38+
else:
39+
pre.next = pre.next.next
40+
return dummy.next
2741
```
2842

2943
### **Java**
3044

3145
```java
32-
46+
/**
47+
* Definition for singly-linked list.
48+
* public class ListNode {
49+
* int val;
50+
* ListNode next;
51+
* ListNode() {}
52+
* ListNode(int val) { this.val = val; }
53+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
54+
* }
55+
*/
56+
class Solution {
57+
public ListNode removeElements(ListNode head, int val) {
58+
ListNode dummy = new ListNode(-1, head);
59+
ListNode pre = dummy;
60+
while (pre != null && pre.next != null) {
61+
if (pre.next.val != val) pre = pre.next;
62+
else pre.next = pre.next.next;
63+
}
64+
return dummy.next;
65+
}
66+
}
3367
```
3468

3569
### **...**

‎solution/0200-0299/0203.Remove Linked List Elements/Solution.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
* public class ListNode {
44
* int val;
55
* ListNode next;
6-
* ListNode(int x) { val = x; }
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
79
* }
810
*/
911
class Solution {
1012
public ListNode removeElements(ListNode head, int val) {
11-
if (head == null) {
12-
return null;
13+
ListNode dummy = new ListNode(-1, head);
14+
ListNode pre = dummy;
15+
while (pre != null && pre.next != null) {
16+
if (pre.next.val != val) pre = pre.next;
17+
else pre.next = pre.next.next;
1318
}
14-
head.next = removeElements(head.next, val);
15-
return head.val != val ? head : head.next;
19+
return dummy.next;
1620
}
17-
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
# Definition for singly-linked list.
22
# class ListNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.next = None
6-
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
76
class Solution:
8-
def removeElements(self, head, val):
9-
"""
10-
:type head: ListNode
11-
:type val: int
12-
:rtype: ListNode
13-
"""
14-
if head == None:
15-
return None
16-
head.next=self.removeElements(head.next,val)
17-
if head.val == val:
18-
return head.next
19-
else:
20-
return head
7+
def removeElements(self, head: ListNode, val: int) -> ListNode:
8+
dummy = ListNode(-1, head)
9+
pre = dummy
10+
while pre and pre.next:
11+
if pre.next.val != val:
12+
pre = pre.next
13+
else:
14+
pre.next = pre.next.next
15+
return dummy.next

0 commit comments

Comments
 (0)