Problem: 1 -> 2 -> 3 ->3 ->3 ->4 -> 4 ->5 should become 1 -> 2 -> 5
public void DeleteDuplicates()
{
if (this.Head == null)
{
return;
}
LinkedListNode dummy = new LinkedListNode(-1, this.Head);
LinkedListNode prev = dummy;
LinkedListNode curr = this.Head;
LinkedListNode next = curr.Next;
while (next != null)
{
if (next.Value == curr.Value)
{
while(next != null && next.Value == curr.Value)
{
next = next.Next;
}
curr = next;
prev.Next = curr;
if (next != null)
{
next = next.Next;
}
}
else
{
prev = prev.Next;
curr = curr.Next;
next = next.Next;
}
}
this.Head = dummy.Next;
}
No comments:
Post a Comment