刪除有序鏈表中重複的元素

好比 1->2->2->3,刪除重複後,變爲1->3。node

void delDup(Node* head)
{
    Node node;
    node.next = head;
    
    Node* n1, n2;
    n1 = node;
    n2 = node->next;
    int data;
    while (n2)
    {
        data = n2->data;
        n2 = n2->next;
        if (n2 && n2->data == data)
        {
            n2 = n2->next;
        }
        else
        {
            if (n1->next->next == n2)
            {
                n1 = n1->next;
            }
            else
            {
                n1->next = n2;
            }
        }
    }
    head = node.next;
}    
相關文章
相關標籤/搜索