Skip to content

Commit f0f00df

Browse files
committed
Add solution 083
1 parent 0917b81 commit f0f00df

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Complete solutions to Leetcode problems, updated daily.
1414
|---|---|---|
1515
| 001 | [Two Sum](https://github.com/yanglbme/leetcode/tree/master/solution/001.Two%20Sum) | `Array`, `Hash Table` |
1616
| 021 | [Merge Two Sorted Lists](https://github.com/yanglbme/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists) | `Linked List` |
17+
| 083 | [Remove Duplicates from Sorted List](https://github.com/yanglbme/leetcode/tree/master/solution/083.Remove%20Duplicates%20from%20Sorted%20List) | `Linked List` |
1718
| 203 | [Remove Linked List Elements](https://github.com/yanglbme/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
1819

1920

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## 删除排序链表中的重复元素
2+
### 题目描述
3+
4+
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
5+
6+
示例 1:
7+
```
8+
输入: 1->1->2
9+
输出: 1->2
10+
```
11+
12+
示例 2:
13+
```
14+
输入: 1->1->2->3->3
15+
输出: 1->2->3
16+
```
17+
18+
### 解法
19+
利用链表的递归性,先判断当前结点的值与下个结点值是否相等,是的话,链表为 deleteDuplicates(ListNode head),否则为 head->deleteDuplicates(head.next)。
20+
21+
```java
22+
/**
23+
* Definition for singly-linked list.
24+
* public class ListNode {
25+
* int val;
26+
* ListNode next;
27+
* ListNode(int x) { val = x; }
28+
* }
29+
*/
30+
class Solution {
31+
public ListNode deleteDuplicates(ListNode head) {
32+
if (head == null || head.next == null) {
33+
return head;
34+
}
35+
36+
head.next = deleteDuplicates(head.next);
37+
return head.val == head.next.val ? head.next : head;
38+
39+
}
40+
}
41+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public ListNode deleteDuplicates(ListNode head) {
3+
if (head == null || head.next == null) {
4+
return head;
5+
}
6+
7+
head.next = deleteDuplicates(head.next);
8+
return head.val == head.next.val ? head.next : head;
9+
10+
}
11+
}

solution/083.Remove Duplicates from Sorted List/Solution.py

Whitespace-only changes.

0 commit comments

Comments
 (0)