Skip to content

Commit 98b6e12

Browse files
committed
Add solution 082
1 parent 0ef0972 commit 98b6e12

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
![LeetCode-GitHub](http://p9ucdlghd.bkt.clouddn.com/leetcode-github.png)
33

44
[![PRs Welcome](https://img.shields.io/badge/PRs-Welcome-brightgreen.svg)](http://makeapullrequest.com)
5-
[![Language](https://img.shields.io/badge/Lang-Java%2FPython-blue.svg)](https://github.com/yanglbme/leetcode)
5+
[![Language](https://img.shields.io/badge/Lang-Java%2FPython%2FJS%2FCPP%2FGo%2F...-blue.svg)](https://github.com/yanglbme/leetcode)
66
## Introduction
77
Complete solutions to Leetcode problems, updated daily.
88

@@ -16,12 +16,15 @@ Complete solutions to Leetcode problems, updated daily.
1616
| 021 | [Merge Two Sorted Lists](https://github.com/yanglbme/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists) | `Linked List` |
1717
| 203 | [Remove Linked List Elements](https://github.com/yanglbme/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
1818

19+
1920
### Medium
2021

2122
| # | Title | Tags |
2223
|---|---|---|
2324
| 019 | [Remove Nth Node From End of List](https://github.com/yanglbme/leetcode/tree/master/solution/019.Remove%20Nth%20Node%20From%20End%20of%20List) | `Linked List`, `Two Pointers` |
2425
| 024 | [Swap Nodes in Pairs](https://github.com/yanglbme/leetcode/tree/master/solution/024.Swap%20Nodes%20in%20Pairs) | `Linked List` |
26+
| 082 | [Remove Duplicates from Sorted List II](https://github.com/yanglbme/leetcode/tree/master/solution/082.Remove%20Duplicates%20from%20Sorted%20List%20II) | `Linked List` |
27+
2528

2629
### Hard
2730

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## 删除链表中的结点
2+
### 题目描述
3+
4+
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中**没有重复出现**的数字。
5+
6+
示例 1:
7+
```
8+
输入: 1->2->3->3->4->4->5
9+
输出: 1->2->5
10+
```
11+
12+
示例 2:
13+
```
14+
输入: 1->1->1->2->3
15+
输出: 2->3
16+
```
17+
18+
### 解法
19+
利用链表的递归性,需要注意处理连续 n(n>=3) 个结点相等的情况。若相邻只有两个结点相等,则直接返回deleteDuplicates(head.next.next);若相邻结点超过 3 个相等,返回 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+
if (head.val == head.next.val) {
37+
if (head.next.next == null) {
38+
return null;
39+
}
40+
if (head.val == head.next.next.val) {
41+
return deleteDuplicates(head.next);
42+
}
43+
return deleteDuplicates(head.next.next);
44+
}
45+
head.next = deleteDuplicates(head.next);
46+
return head;
47+
}
48+
}
49+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public ListNode deleteDuplicates(ListNode head) {
3+
if (head == null || head.next == null) {
4+
return head;
5+
}
6+
7+
if (head.val == head.next.val) {
8+
if (head.next.next == null) {
9+
return null;
10+
}
11+
if (head.val == head.next.next.val) {
12+
return deleteDuplicates(head.next);
13+
}
14+
return deleteDuplicates(head.next.next);
15+
}
16+
head.next = deleteDuplicates(head.next);
17+
return head;
18+
}
19+
}

solution/082.Remove Duplicates from Sorted List II/Solution.py

Whitespace-only changes.

0 commit comments

Comments
 (0)