82. Remove Duplicates from Sorted List II
Last updated
Last updated
Input: head = [1,2,3,3,4,4,5]
Output: [1,2,5]Input: head = [1,1,1,2,3]
Output: [2,3]// code here.
func deleteDuplicates(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
dummyHead := &ListNode{-114514, head}
pre := dummyHead
for cur := head; cur != nil && cur.Next != nil; cur = cur.Next {
if cur.Val != cur.Next.Val {
pre = cur
continue
}
next := cur.Next
for next != nil && cur.Val == next.Val {
next = next.Next
}
pre.Next = next
cur = pre
}
return dummyHead.Next
}