本题目测试中给出了 初始化链表 和 判断两个链表是否相同 的方法
题目
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
解题思路
遍历链表,去掉重复的元素
注意
go语言不需要回收内存
代码
removeDuplicates.go
package _83_Remove_Duplicates_from_Sorted_List
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
type ListNode struct {
Val int
Next *ListNode
}
func DeleteDuplicates(head *ListNode) *ListNode {
if nil == head {
return nil
}
newHead := head
p := head
q := head.Next
for ; q != nil ; {
if p.Val == q.Val {
p.Next = q.Next
q.Next = nil
q = p.Next
} else {
p = q
q = q.Next
}
}
return newHead
}
测试
removeDuplicates_test.go
package _83_Remove_Duplicates_from_Sorted_List
import "testing"
func InitList(nums []int) (head *ListNode) {
len1 := len(nums)
if len1 == 0 {
return nil
}
head = new(ListNode)
head.Val = nums[0]
head.Next = nil
p := head
for i := 1; i < len1; i++ {
q := new(ListNode)
q.Val = nums[i]
q.Next = nil
p.Next = q
p = q
}
return head
}
func ListEqual(list1, list2 *ListNode) bool {
if nil == list1 && nil == list2 {
return true
} else if nil != list1 && nil == list2 {
return false
} else if nil == list1 && nil != list2 {
return false
}
for ; nil != list1 && nil != list2; {
if list1.Val != list2.Val {
return false
}
list1 = list1.Next
list2 = list2.Next
}
if nil == list1 && nil == list2 {
return true
}
return false
}
func TestDeleteDuplicates(t *testing.T) {
input1 := []int{1, 1, 2, 3, 3}
inputList := InitList(input1)
output1 := []int{1, 2, 3}
outputList := InitList(output1)
var tests = []struct{
input *ListNode
output *ListNode
} {
{inputList, outputList},
}
for _, v := range tests {
ret := DeleteDuplicates(v.input)
ok := ListEqual(ret, v.output)
if ok {
t.Logf("pass")
} else {
t.Errorf("fail, want %+v, get %+v\n", v.output, ret)
}
}
}