c - Why doesn't following code work for a linked list with just 2 nodes having same value? -


the following function deletes duplicates in unsorted linked list. works cases except when linked list has 2 nodes have duplicate values such 10->10>null can point me going wrong ?

void deldup(struct node* head) {     struct node* outer = head;     struct node* inner, *temp;      while (outer->next != null && outer != null)     {         inner = outer;         while (inner != null && inner->next != null)         {             if (inner->next->data == outer->data)             {                 temp = inner->next;                 inner->next = inner->next->next;                 free(temp);             }             else             {                 inner = inner->next;             }         }         outer = outer->next;     } } 

after deletion of second 10, outer->next null gets assigned outer. next outer while loop condition tries access outer->next first crashes.

solution: reverse outer->next != null && outer != nullinto outer != null && outer->next != null. remember, second part of logical , isn't evaluated if first 1 false already, eactly scenarios one.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -