Skip to content

Commit b8e5a56

Browse files
committed
added problem 83
1 parent 2008baa commit b8e5a56

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package problem0083
2+
3+
import . "leetcodedaily/helpers/listnode"
4+
5+
/*
6+
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
7+
*/
8+
9+
func deleteDuplicates(head *ListNode) *ListNode {
10+
if head == nil {
11+
return nil
12+
}
13+
for cur := head; cur.Next != nil; {
14+
if cur.Val == cur.Next.Val {
15+
cur.Next = cur.Next.Next
16+
} else {
17+
cur = cur.Next
18+
}
19+
}
20+
return head
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package problem0083
2+
3+
import (
4+
"fmt"
5+
. "leetcodedaily/helpers/listnode"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
type Result struct {
12+
Input *ListNode
13+
Expected *ListNode
14+
}
15+
16+
var Results = []Result{
17+
{MakeListNode(1, 1, 2), MakeListNode(1, 2)},
18+
{MakeListNode(1, 1, 2, 3, 3), MakeListNode(1, 2, 3)},
19+
{MakeListNode(1), MakeListNode(1)},
20+
{MakeListNode(), MakeListNode()},
21+
{MakeListNode(1, 2), MakeListNode(1, 2)},
22+
}
23+
24+
func TestRemoveListDuplicates(t *testing.T) {
25+
assert := assert.New(t)
26+
27+
for _, res := range Results {
28+
want := res.Expected
29+
got := deleteDuplicates(res.Input)
30+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
31+
}
32+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Each problem is in it's own directory, with test files. There are helper package
5757
| 0076 | [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring) | [My Solution](./problems/problem0076) ||
5858
| 0077 | [Combinations](https://leetcode.com/problems/combinations) | [My Solution](./problems/problem0077) ||
5959
| 0079 | [Word Search](https://leetcode.com/problems/word-search) | [My Solution](./problems/problem0079) ||
60+
| 0083 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list) | [My Solution](./problems/problem0083) ||
6061
| 0086 | [Partition List](https://leetcode.com/problems/partition-list) | [My Solution](./problems/problem0086) ||
6162
| 0088 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array) | [My Solution](./problems/problem0088) ||
6263
| 0091 | [Decode Ways](https://leetcode.com/problems/decode-ways) | [My Solution](./problems/problem0091) ||

0 commit comments

Comments
 (0)