82. Remove Duplicates from Sorted List II

Posted wesson2019

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了82. Remove Duplicates from Sorted List II相关的知识,希望对你有一定的参考价值。

LeetCode、链表
SLinkedList<int> slist = new SLinkedList<int>();
slist.AppendRange(new[]  6, 1, 1, 2, 3, 3, 3, 4, 5, 5 );
Console.WriteLine("Before: " + slist.Print());
var rslt = slist.DeleteDuplicates();
Console.WriteLine(" After: " + rslt.Print());

/// <summary>
/// 删除链表中重复的结点,只要是有重复过的结点,全部删除。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static SLinkedList<T> DeleteDuplicates<T>(this SLinkedList<T> source) where T : IComparable<T>

    if (source.IsEmpty())
    
        return null;
    
    if (source.Head == null || source.Head.Next == null)
    
        return source;
    
    var head = DeleteDuplicates(source.Head);
    return new SLinkedList<T>(head);

private static SLinkedListNode<T> DeleteDuplicates<T>(SLinkedListNode<T> head) where T : IComparable<T>

    if (head == null)
    
        return null;
    
    if (head.Next != null && head.IsEqualTo(head.Next))
    
        while (head.Next != null && head.IsEqualTo(head.Next))
        
            head = head.Next;
        
        return DeleteDuplicates(head.Next);
    
    head.Next = DeleteDuplicates(head.Next);
    return head;

以上是关于82. Remove Duplicates from Sorted List II的主要内容,如果未能解决你的问题,请参考以下文章

82. Remove Duplicates from Sorted List II

82. Remove Duplicates from Sorted List II(js)

82. Remove Duplicates from Sorted List II

82. Remove Duplicates from Sorted List II

82. Remove Duplicates from Sorted List II

82. Remove Duplicates from Sorted List II