题目:
给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。
算法:
func deleteDuplicates(head *ListNode) *ListNode {
cur, next := head, head
for cur != nil {
for next != nil && cur.Val == next.Val {
next = next.Next
}
// next指向nil或者下一个不等于cur的位置
cur.Next = next
cur = next
if next != nil {
next = next.Next
}
}
return head
}