Skip to content

Commit 91b010a

Browse files
committed
Add solution 203
1 parent 213c3b3 commit 91b010a

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Complete solutions to Leetcode problems, updated daily.
1313
| # | Title | Tags |
1414
|---|---|---|
1515
| 001 | [Two Sum](https://github.com/yanglbme/leetcode/tree/master/solution/001.Two%20Sum) | `Array`, `Hash Table` |
16+
| 203 | [Remove Linked List Elements](https://github.com/yanglbme/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
1617

1718
### Medium
1819

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## 删除链表中的结点
2+
### 题目描述
3+
4+
删除链表中等于给定值 val 的所有节点。
5+
6+
示例:
7+
```
8+
输入: 1->2->6->3->4->5->6, val = 6
9+
输出: 1->2->3->4->5
10+
```
11+
12+
### 解法
13+
利用链表天然的递归性。首先判断头结点 `head` 的值是否为 val,若是,那么最终链表是 head->removeElements(head.next, val);若不是,则为 removeElements(head.next, val);
14+
15+
```java
16+
/**
17+
* Definition for singly-linked list.
18+
* public class ListNode {
19+
* int val;
20+
* ListNode next;
21+
* ListNode(int x) { val = x; }
22+
* }
23+
*/
24+
class Solution {
25+
public ListNode removeElements(ListNode head, int val) {
26+
if (head == null) {
27+
return null;
28+
}
29+
head.next = removeElements(head.next, val);
30+
return head.val != val ? head : head.next;
31+
}
32+
}
33+
```

solution/203.Remove Linked List Elements/solution.java

Whitespace-only changes.

0 commit comments

Comments
 (0)