```
public ListNode DeleteDuplicates(ListNode head) {
ListNode prev = null;
ListNode node = head;
while (node != null)
{
if (node.next != null && node.val == node.next.val)
{
int duplicatedValue = node.val;
do { node = node.next; } while (node.next != null && duplicatedValue == node.next.val);
if (prev == null)
head = node.next;
else
prev.next = node.next;
}
else
prev = node;
node = node.next;
}
return head;
}
```