File tree 6 files changed +171
-2
lines changed
solution/1-99/0083.remove-duplicates-from-sorted-list
6 files changed +171
-2
lines changed Original file line number Diff line number Diff line change 19
19
- [ 28.实现 strStr() ✅] ( solution/1-99/0028.implement-strstr/ )
20
20
- [ 35.搜索插入位置 ✅] ( solution/1-99/0035.search-insert-position/ )
21
21
- [ 66.加一 ✅] ( solution/1-99/0066.plus-one/ )
22
- - [ 88.合并两个有序数组] ( solution/1-99/0088.merge-sorted-array/ )
22
+ - [ 83.删除排序链表中的重复元素 ✅] ( solution/1-99/0083.remove-duplicates-from-sorted-list/ )
23
+ - [ 88.合并两个有序数组 ✅] ( solution/1-99/0088.merge-sorted-array/ )
23
24
- [ 125.验证回文串 ✅] ( solution/100-199/0125.valid-palindrome/ )
24
25
25
26
Original file line number Diff line number Diff line change 59
59
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
60
60
| --- | --- | --- | --- | --- |
61
61
| [ 21] ( https://leetcode-cn.com/problems/merge-two-sorted-lists ) | [ 合并两个有序链表] ( /solution/1-99/0021.merge-two-sorted-lists/ ) | ` 链表 ` | <font color =green >简单</font > | ✅ |
62
+ | [ 83] ( https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list ) | [ 删除排序链表中的重复元素] ( /solution/1-99/0083.remove-duplicates-from-sorted-list/ ) | ` 链表 ` | <font color =green >简单</font > | ✅ |
62
63
63
64
#### ** 二分查找**
64
65
Original file line number Diff line number Diff line change 25
25
| [ 67] ( https://leetcode-cn.com/problems/add-binary ) | [ 二进制求和] ( /solution/1-99/0067.add-binary/ ) | ` 数学 ` ,` 字符串 ` | <font color =green >简单</font > |
26
26
| [ 69] ( https://leetcode-cn.com/problems/sqrtx ) | [ x 的平方根] ( /solution/1-99/0069.sqrt%28x%29/ ) | ` 数学 ` ,` 二分查找 ` | <font color =green >简单</font > |
27
27
| [ 70] ( https://leetcode-cn.com/problems/climbing-stairs ) | [ 爬楼梯] ( /solution/1-99/0070.climbing-stairs/ ) | ` 动态规划 ` | <font color =green >简单</font > |
28
- | [ 83] ( https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list ) | [ 删除排序链表中的重复元素] ( /solution/1-99/0083.remove-duplicates-from-sorted-list/ ) | ` 链表 ` | <font color =green >简单</font > |
28
+ | [ 83] ( https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list ) | [ 删除排序链表中的重复元素] ( /solution/1-99/0083.remove-duplicates-from-sorted-list/ ) | ` 链表 ` | <font color =green >简单</font > | ✅ |
29
29
| [ 88] ( https://leetcode-cn.com/problems/merge-sorted-array ) | [ 合并两个有序数组] ( /solution/1-99/0088.merge-sorted-array/ ) | ` 数组 ` ,` 双指针 ` | <font color =green >简单</font > | ✅ |
30
30
| [ 100] ( https://leetcode-cn.com/problems/same-tree ) | [ 相同的树] ( /solution/100-199/0100.same-tree/ ) | ` 树 ` ,` 深度优先搜索 ` | <font color =green >简单</font > |
31
31
| [ 101] ( https://leetcode-cn.com/problems/symmetric-tree ) | [ 对称二叉树] ( /solution/100-199/0101.symmetric-tree/ ) | ` 树 ` ,` 深度优先搜索 ` ,` 广度优先搜索 ` | <font color =green >简单</font > |
Original file line number Diff line number Diff line change
1
+ # [ 83.删除排序链表中的重复元素] ( https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/ )
2
+
3
+
4
+ ### 题目描述
5
+
6
+ <div class =" notranslate " ><p >给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。</p >
7
+
8
+ <p ><strong >示例  ; 1:</strong ></p >
9
+
10
+ <pre ><strong >输入:</strong > 1-> ; 1-> ; 2
11
+ <strong >输出:</strong > 1-> ; 2
12
+ </pre >
13
+
14
+ <p ><strong >示例  ; 2:</strong ></p >
15
+
16
+ <pre ><strong >输入:</strong > 1-> ; 1-> ; 2-> ; 3-> ; 3
17
+ <strong >输出:</strong > 1-> ; 2-> ; 3</pre >
18
+ </div >
19
+
20
+ ### 解题思路
21
+
22
+ 1 . 当前节点的下一个指针不为空时,判断当前节点的值跟下一个指针节点的值是否相等。
23
+ 2 . 如果相等,则将下下个节点的指针指向当前节点的下一个指针。
24
+ 3 . 如果不相等,则指针往前移。
25
+
26
+ ### 代码实现
27
+
28
+ <!-- tabs:start -->
29
+
30
+ #### ** Golang**
31
+ ``` go
32
+ type ListNode struct {
33
+ Val int
34
+ Next *ListNode
35
+ }
36
+
37
+ func deleteDuplicates (head *ListNode ) *ListNode {
38
+ if head == nil {
39
+ return nil
40
+ }
41
+ ret := head
42
+ for head.Next != nil {
43
+ if head.Val == head.Next .Val {
44
+ head.Next = head.Next .Next
45
+ continue
46
+ }
47
+ head = head.Next
48
+ }
49
+ return ret
50
+ }
51
+ ```
52
+
53
+
54
+ <!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ package leetcode
2
+
3
+ /*
4
+ * @lc app=leetcode.cn id=83 lang=golang
5
+ *
6
+ * [83] 删除排序链表中的重复元素
7
+ */
8
+
9
+ type ListNode struct {
10
+ Val int
11
+ Next * ListNode
12
+ }
13
+
14
+ // @lc code=start
15
+ /**
16
+ * Definition for singly-linked list.
17
+ * type ListNode struct {
18
+ * Val int
19
+ * Next *ListNode
20
+ * }
21
+ */
22
+ func deleteDuplicates (head * ListNode ) * ListNode {
23
+ if head == nil {
24
+ return nil
25
+ }
26
+ ret := head
27
+ for head .Next != nil {
28
+ if head .Val == head .Next .Val {
29
+ head .Next = head .Next .Next
30
+ continue
31
+ }
32
+ head = head .Next
33
+ }
34
+ return ret
35
+ }
36
+
37
+ // @lc code=end
Original file line number Diff line number Diff line change
1
+ package leetcode
2
+
3
+ import (
4
+ "fmt"
5
+ "testing"
6
+ )
7
+
8
+ func TestDeleteDuplicates (t * testing.T ) {
9
+ t .Run ("Test-1" , func (t * testing.T ) {
10
+ data1 := []int {1 , 1 , 2 }
11
+ got := deleteDuplicates (MakeListNote (data1 ))
12
+ want := MakeListNote ([]int {1 , 2 })
13
+
14
+ if ! Equal (got , want ) {
15
+ fmt .Print ("GOT:" )
16
+ PrintList (got )
17
+ fmt .Print ("WANT:" )
18
+ PrintList (want )
19
+ t .Error ("GOT:" , got , "WANT:" , want )
20
+ }
21
+ })
22
+
23
+ t .Run ("Test-2" , func (t * testing.T ) {
24
+ got := deleteDuplicates (& ListNode {})
25
+ want := & ListNode {}
26
+ if ! Equal (got , want ) {
27
+ fmt .Print ("GOT:" )
28
+ PrintList (got )
29
+ fmt .Print ("WANT:" )
30
+ PrintList (want )
31
+ t .Error ("GOT:" , got , "WANT:" , want )
32
+ }
33
+ })
34
+ }
35
+
36
+ func Equal (x , y * ListNode ) bool {
37
+ for x == nil || y == nil {
38
+ if x == nil && y != nil {
39
+ fmt .Println (x , y )
40
+ return false
41
+ }
42
+ if x != nil && y == nil {
43
+ fmt .Println (x , y )
44
+ return false
45
+ }
46
+
47
+ if x .Val != y .Val {
48
+ fmt .Println (x , y )
49
+ return false
50
+ }
51
+ x = x .Next
52
+ y = y .Next
53
+ }
54
+ return true
55
+ }
56
+
57
+ func PrintList (x * ListNode ) {
58
+ for x != nil {
59
+ fmt .Print (x .Val , " " )
60
+ x = x .Next
61
+ }
62
+ fmt .Println ()
63
+ }
64
+
65
+ func MakeListNote (x []int ) * ListNode {
66
+ list := & ListNode {}
67
+ head := list
68
+
69
+ list .Val = x [0 ]
70
+ for i := 1 ; i < len (x ); i ++ {
71
+ list .Next = & ListNode {}
72
+ list = list .Next
73
+ list .Val = x [i ]
74
+ }
75
+ return head
76
+ }
You can’t perform that action at this time.
0 commit comments