int[] arr = new[] { 6, 1, 1, 2, 3, 3, 3, 4, 5, 5 };
SLinkedList slist = new SLinkedList();
slist.AppendRange(arr);
Console.WriteLine("Before: " + slist.Print());
var rslt = slist.DistinctDuplicates();
Console.WriteLine(" After: " + rslt.Print());
///
/// 删除链表中重复的结点,每个结点只出现一次。
///
///
///
///
public static SLinkedList DistinctDuplicates(this SLinkedList source)where T : IComparable
{
if (source.IsEmpty())
{
return null;
}
if (source.Head == null || source.Head.Next == null)
{
return source;
}
var tmp = new SLinkedList(source);
var head = DistinctDuplicates(tmp.Head);
tmp.Clear();
return new SLinkedList(head);
}
private static SLinkedListNode DistinctDuplicates(SLinkedListNode head) where T : IComparable
{
if (head == null || head.Next == null)
{
return head;
}
var cur = head;
while (cur.Next != null)
{
if (cur.IsEqualTo(cur.Next))
{
cur.Next = cur.Next.Next;
}
else
{
cur = cur.Next;
}
}
return head;
}