crack the code interview 2.1

Write code to remove duplicates from an unsorted linked list.

不断的如果当前元素和前面的重复,删除当前元素。

struct Node
{
    Node * next;
    int value;
};

void deleteDupNode(Node * head)
{
    if (head == NULL)
        return;
    Node * currentNode = head->next;
    Node * previous = head;
    while (currentNode != NULL)
    {
        Node * start = head;
        while (start != currentNode)
        {
            if (start->value == currentNode->value)
            {
                previous->next = currentNode->next;
                Node * cur = currentNode;
                delete cur;
                break;
            }
        }
        previous = currentNode;
        currentNode = currentNode->next;
    }
}


你可能感兴趣的:(crack,the,code,interview)