-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
aQua
authored and
aQua
committed
Sep 6, 2017
1 parent
9bc9523
commit c651843
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
Algorithms/0082.remove-duplicates-from-sorted-list-ii/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# [82. Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) | ||
|
||
## 题目 | ||
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. | ||
|
||
``` | ||
For example, | ||
Given 1->2->3->3->4->4->5, return 1->2->5. | ||
Given 1->1->1->2->3, return 2->3. | ||
``` | ||
## 解题思路 | ||
|
||
见程序注释 |
12 changes: 12 additions & 0 deletions
12
...ithms/0082.remove-duplicates-from-sorted-list-ii/remove-duplicates-from-sorted-list-ii.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package Problem0082 | ||
|
||
// ListNode is singly-linked list. | ||
type ListNode struct { | ||
Val int | ||
Next *ListNode | ||
} | ||
|
||
func deleteDuplicates(head *ListNode) *ListNode { | ||
|
||
return head | ||
} |
98 changes: 98 additions & 0 deletions
98
.../0082.remove-duplicates-from-sorted-list-ii/remove-duplicates-from-sorted-list-ii_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package Problem0082 | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type question struct { | ||
para | ||
ans | ||
} | ||
|
||
// para 是参数 | ||
type para struct { | ||
head []int | ||
} | ||
|
||
// ans 是答案 | ||
type ans struct { | ||
one []int | ||
} | ||
|
||
func Test_Problem0082(t *testing.T) { | ||
ast := assert.New(t) | ||
|
||
qs := []question{ | ||
|
||
question{ | ||
para{ | ||
[]int{1, 2, 3, 3, 4, 4, 5}, | ||
}, | ||
ans{ | ||
[]int{1, 2, 5}, | ||
}, | ||
}, | ||
|
||
question{ | ||
para{ | ||
[]int{1, 1, 2, 2, 3, 3, 4, 4, 5}, | ||
}, | ||
ans{ | ||
[]int{5}, | ||
}, | ||
}, | ||
|
||
question{ | ||
para{ | ||
[]int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}, | ||
}, | ||
ans{ | ||
[]int{}, | ||
}, | ||
}, | ||
|
||
// 如需多个测试,可以复制上方元素。 | ||
} | ||
|
||
for _, q := range qs { | ||
a, p := q.ans, q.para | ||
fmt.Printf("~~%v~~\n", p) | ||
|
||
ast.Equal(a.one, l2s(deleteDuplicates(s2l(p.head))), "输入:%v", p) | ||
} | ||
} | ||
|
||
// convert *ListNode to []int | ||
func l2s(head *ListNode) []int { | ||
res := []int{} | ||
|
||
for head != nil { | ||
res = append(res, head.Val) | ||
head = head.Next | ||
} | ||
|
||
return res | ||
} | ||
|
||
// convert []int to *ListNode | ||
func s2l(nums []int) *ListNode { | ||
if len(nums) == 0 { | ||
return nil | ||
} | ||
|
||
res := &ListNode{ | ||
Val: nums[0], | ||
} | ||
temp := res | ||
for i := 1; i < len(nums); i++ { | ||
temp.Next = &ListNode{ | ||
Val: nums[i], | ||
} | ||
temp = temp.Next | ||
} | ||
|
||
return res | ||
} |