mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
添加 problem 83
This commit is contained in:
23
Algorithms/83.Remove Duplicates from Sorted List/README.md
Normal file
23
Algorithms/83.Remove Duplicates from Sorted List/README.md
Normal file
@ -0,0 +1,23 @@
|
||||
# [83. Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given a sorted linked list, delete all duplicates such that each element appear only once.
|
||||
|
||||
Example 1:
|
||||
|
||||
```
|
||||
Input: 1->1->2
|
||||
Output: 1->2
|
||||
```
|
||||
|
||||
Example 2:
|
||||
|
||||
```
|
||||
Input: 1->1->2->3->3
|
||||
Output: 1->2->3
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
删除链表中重复的结点,以保障每个结点只出现一次。
|
@ -0,0 +1,27 @@
|
||||
package leetcode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
* Val int
|
||||
* Next *ListNode
|
||||
* }
|
||||
*/
|
||||
|
||||
func deleteDuplicates(head *ListNode) *ListNode {
|
||||
cur := head
|
||||
if head == nil {
|
||||
return nil
|
||||
}
|
||||
if head.Next == nil {
|
||||
return head
|
||||
}
|
||||
for cur.Next != nil {
|
||||
if cur.Next.Val == cur.Val {
|
||||
cur.Next = cur.Next.Next
|
||||
} else {
|
||||
cur = cur.Next
|
||||
}
|
||||
}
|
||||
return head
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question83 struct {
|
||||
para83
|
||||
ans83
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para83 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans83 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem83(t *testing.T) {
|
||||
|
||||
qs := []question83{
|
||||
|
||||
question83{
|
||||
para83{[]int{1, 1, 2}},
|
||||
ans83{[]int{1, 2}},
|
||||
},
|
||||
|
||||
question83{
|
||||
para83{[]int{1, 1, 2, 2, 3, 3, 3}},
|
||||
ans83{[]int{1, 2, 3}},
|
||||
},
|
||||
|
||||
question83{
|
||||
para83{[]int{1, 1, 1, 1, 1, 1, 1, 1}},
|
||||
ans83{[]int{1}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 83------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans83, q.para83
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(deleteDuplicates(S2l(p.one))))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
Reference in New Issue
Block a user