Hacker News new | past | comments | ask | show | jobs | submit login

When I was an undergrad learning linked lists I discovered its possible to remove a node from a singly linked list with only a reference to the node to delete. This is cool because much of the point of a doubly-linked list is to enable this but it takes another pointer on every node to allow it. My idea was instead of deleting the node itself, just delete the next one and fix up the data pointers.

Like:

    remove(node)
    {
        next = node->next;
        node->data = next->data;
        node->next = next->next;
        free(next);
    }

You need to handle the case of deleting the last element in the list but that can be done by keeping an EOL element at the end just for this purpose. It's surprising the me that this solution is not very well known.



Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: