82. Remove Duplicates from Sorted List II
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
Python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(val=None, next=head)
pre, cur = dummy, head
while cur:
if cur.next and cur.val == cur.next.val:
# Move over all duplicates
while cur.next and cur.val == cur.next.val:
cur = cur.next
pre.next = cur.next
else:
pre = pre.next
cur = cur.next
return dummy.next
Go
func deleteDuplicates(head *ListNode) *ListNode {
dummy := new(ListNode)
dummy.Next = head
pre, cur := dummy, head
for cur != nil {
if cur.Next != nil && cur.Val == cur.Next.Val {
for cur.Next != nil && cur.Val == cur.Next.Val {
cur = cur.Next
}
pre.Next = cur.Next
} else {
pre = pre.Next
}
cur = cur.Next
}
return dummy.Next
}