[Solved] I wrote this method to delete duplicate elements of a linked list, but it shows segmentation fault, why? [closed]


Look at this code:

        if (a == b)
        {
            while (a == b)
            {
                ptr->next = tmp->next;
                tmp = tmp->next;
            }

When a is equal to b you execute the while (a == b). Since you don’t change a or b in the body of the while, you have an endless loop.

Sooner or later tmp will be NULL and your program crashes.

You probably wanted to update either a or b inside the loop. Further you need to check for NULL before doing ptr->next = tmp->next

solved I wrote this method to delete duplicate elements of a linked list, but it shows segmentation fault, why? [closed]