Skip to content

Commit

Permalink
82 added
Browse files Browse the repository at this point in the history
  • Loading branch information
aQua authored and aQua committed Sep 6, 2017
1 parent 9bc9523 commit c651843
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Algorithms/0082.remove-duplicates-from-sorted-list-ii/README.md
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.
```
## 解题思路

见程序注释
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
}
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
}

0 comments on commit c651843

Please sign in to comment.