Skip to content

Commit ac4d662

Browse files
committed
三刷82
1 parent 4458bb6 commit ac4d662

File tree

7 files changed

+109
-14
lines changed

7 files changed

+109
-14
lines changed

docs/0000-07-fast-slow-pointers.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Middle of the LinkedList (easy)
2424

2525
== 经典题目
2626

27+
. xref:0082-remove-duplicates-from-sorted-list-ii.adoc[82. Remove Duplicates from Sorted List II]
2728
. xref:0141-linked-list-cycle.adoc[141. Linked List Cycle]
2829
. xref:0142-linked-list-cycle-ii.adoc[142. Linked List Cycle II]
2930
. xref:0143-reorder-list.adoc[143. Reorder List]

docs/0082-remove-duplicates-from-sorted-list-ii.adoc

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,73 @@
11
[#0082-remove-duplicates-from-sorted-list-ii]
2-
= 82. Remove Duplicates from Sorted List II
2+
= 82. 删除排序链表中的重复元素 II
33

4-
{leetcode}/problems/remove-duplicates-from-sorted-list-ii/[LeetCode - Remove Duplicates from Sorted List II^]
4+
https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/[LeetCode - 82. 删除排序链表中的重复元素 II ^]
55

6-
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
6+
给定一个已排序的链表的头 `head`_删除原始链表中所有重复数字的节点,只留下不同的数字_ 。返回 _已排序的链表_
77

8-
.Example 1:
9-
----
10-
Input: 1->2->3->3->4->4->5
11-
Output: 1->2->5
12-
----
138

14-
.Example 2:
15-
----
16-
Input: 1->1->1->2->3
17-
Output: 2->3
18-
----
9+
*示例 1:*
10+
11+
image::images/0082-01.jpg[{image_attr}]
12+
13+
....
14+
输入:head = [1,2,3,3,4,4,5]
15+
输出:[1,2,5]
16+
....
17+
18+
*示例 2:*
19+
20+
image::images/0082-02.jpg[{image_attr}]
21+
22+
....
23+
输入:head = [1,1,1,2,3]
24+
输出:[2,3]
25+
....
1926

20-
== 解题分析
27+
*提示:*
28+
29+
* 链表中节点数目在范围 `[0, 300]`
30+
* `+-100 <= Node.val <= 100+`
31+
* 题目数据保证链表已经按升序 *排列*
32+
33+
34+
== 思路分析
2135

2236
快慢指针是个好办法!
2337

2438
注意推敲满指针前进的条件!
2539

2640
[[src-0082]]
41+
[tabs]
42+
====
43+
一刷::
44+
+
45+
--
2746
[{java_src_attr}]
2847
----
2948
include::{sourcedir}/_0082_RemoveDuplicatesFromSortedListII.java[tag=answer]
3049
----
50+
--
3151
52+
二刷::
53+
+
54+
--
3255
[{java_src_attr}]
3356
----
3457
include::{sourcedir}/_0082_RemoveDuplicatesFromSortedListII_2.java[tag=answer]
3558
----
59+
--
60+
61+
三刷::
62+
+
63+
--
64+
[{java_src_attr}]
65+
----
66+
include::{sourcedir}/_0082_RemoveDuplicatesFromSortedListIi_3.java[tag=answer]
67+
----
68+
--
69+
====
70+
3671

3772
== 参考资料
3873

docs/images/0082-01.jpg

19 KB
Loading

docs/images/0082-02.jpg

13.6 KB
Loading

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,11 @@ endif::[]
733733
|{doc_base_url}/0055-jump-game.adoc[题解]
734734
|✅ 贪心算法。没想到这就是贪心?
735735

736+
|{counter:codes2503}
737+
|{leetcode_base_url}/remove-duplicates-from-sorted-list-ii/[82. 删除排序链表中的重复元素 II^]
738+
|{doc_base_url}/0082-remove-duplicates-from-sorted-list-ii.adoc[题解]
739+
|✅ 快慢指针。
740+
736741
|===
737742

738743
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0082_RemoveDuplicatesFromSortedListII_2.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public class _0082_RemoveDuplicatesFromSortedListII_2 {
1515

1616
/**
1717
* 参考官方题解
18+
*
19+
* @author D瓜哥 · https://www.diguage.com
20+
* @since 2024-07-03 16:50:50
1821
*/
1922
public ListNode deleteDuplicates(ListNode head) {
2023
if (head == null || head.next == null) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.ListNode;
4+
import com.diguage.util.ListNodes;
5+
6+
import java.util.Arrays;
7+
8+
public class _0082_RemoveDuplicatesFromSortedListIi_3 {
9+
// tag::answer[]
10+
11+
/**
12+
* @author D瓜哥 · https://www.diguage.com
13+
* @since 2025-05-20 14:39:15
14+
*/
15+
public ListNode deleteDuplicates(ListNode head) {
16+
ListNode dummy = new ListNode(101);
17+
dummy.next = head;
18+
ListNode pre = dummy;
19+
ListNode slow = head;
20+
ListNode fast = head;
21+
boolean flag = false;
22+
while (fast != null) {
23+
if (fast.val == slow.val) {
24+
if (fast != slow) {
25+
flag = true;
26+
}
27+
fast = fast.next;
28+
} else {
29+
if (flag) {
30+
pre.next = fast;
31+
slow = fast;
32+
flag = false;
33+
} else {
34+
pre = pre.next;
35+
slow = slow.next;
36+
fast = fast.next;
37+
}
38+
}
39+
}
40+
if (flag) {
41+
pre.next = fast;
42+
}
43+
return dummy.next;
44+
}
45+
// end::answer[]
46+
47+
public static void main(String[] args) {
48+
new _0082_RemoveDuplicatesFromSortedListIi_3()
49+
.deleteDuplicates(ListNodes.build(Arrays.asList(1, 1)));
50+
}
51+
}

0 commit comments

Comments
 (0)